有人能检查一下这个密码的问题吗?我已经实现了一个服务并将一个BluetoothDevice对象传递给它(通过intent的BluetoothDevice对象没有错误/问题)。然后,在onStartCommand()中,我调用deviceToConnect.connectGatt(this,false,mGattCallback)。但我的BluetoothGattCallback()没有工作(没有打印任何东西)。代码简单明了。有人能帮我调试一下吗?
编辑:我正在MainActivity()中执行le device scan,并将设备对象传递给服务以连接到设备。

public class PairedBleService extends Service
{
    private BluetoothGatt mConnectedGatt;
    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        super.onStartCommand(intent, flags, startId);

        BluetoothDevice deviceToConnect = (BluetoothDevice) intent.getParcelableExtra(DEVICE_TO_CONNECT);
        mConnectedGatt = deviceToConnect.connectGatt(this, false, mGattCallback);

        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        Toast.makeText(this, "Service End", Toast.LENGTH_SHORT).show();
        super.onDestroy();
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    private BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {

    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        if (status == BluetoothGatt.GATT_SUCCESS && newState == BluetoothProfile.STATE_CONNECTED) {
            Toast.makeText(getApplicationContext(), "Peripheral connected", Toast.LENGTH_LONG).show();
        } else if (status == BluetoothGatt.GATT_SUCCESS && newState == BluetoothProfile.STATE_DISCONNECTED) {
            Toast.makeText(getApplicationContext(), "Peripheral Disconnected", Toast.LENGTH_LONG).show();
        } else if (status != BluetoothGatt.GATT_SUCCESS) {
            gatt.disconnect();
        }
    }
}

编辑:我试图连接我的信标(外设)与标准的android蓝牙s/w,在那里我可以进行连接。但在那里,它要求配对引脚,一旦把引脚是连接和显示在配对蓝牙设备。有没有类似于connectGatt的方法可以向用户询问“配对密码”…我无法理解我缺少了什么。

最佳答案

onbind方法返回null,因此主活动无法与服务通信。
你的代码应该如下所示,
@配对服务

public class LocalBinder extends Binder
{
    PairedBleService getService()
    {
        return PairedBleService.this;
    };
};

@Override
public IBinder onBind(Intent intent)
{
    // TODO Auto-generated method stub
    return mBinder;
};

private final IBinder mBinder = new LocalBinder();

@主要活动
//put this inside onServiceConnected

PairedBleService bleService = ((PairedBleService.LocalBinder) service).getService();

// Your code for connecting with BLE device
....................................
....................................

07-27 13:40