我正在尝试编写一个通过蓝牙低能耗发送消息的应用程序,然后这些消息将通过我外围设备中的UART传递。我已经按照here和应用程序扫描并成功找到设备的步骤进行了操作。但是,使用BluetoothGatt = BluetoothDevice.connectGatt(context,autoconnect,callback)方法连接失败,logcat显示“注册回调失败”。
从以下位置发出呼叫:
//device scan callback
private BluetoothAdapter.LeScanCallback btScanCallback = new BluetoothAdapter.LeScanCallback()
{
@Override
public void onLeScan(final BluetoothDevice device, final int rssi, final byte[] scanRecord)
{
some stuff
currBtGatt = device.connectGatt(parentActivity, false, btGattCallback);
}
};
//GATT callback
private BluetoothGattCallback btGattCallback = new BluetoothGattCallback()
{
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState)
{
// if connected successfully
if(newState == BluetoothProfile.STATE_CONNECTED)
{
//discover services
updateStatus("Connected");
gatt.discoverServices();
}
else if(newState == BluetoothProfile.STATE_DISCONNECTED)
{
updateStatus("Disconnected");
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status)
{
if(status == BluetoothGatt.GATT_SUCCESS)
{
//pick out the (app side) transmit channel
currBtService = gatt.getService(uartUuids[0]);
currBtCharacteristic = currBtService.getCharacteristic(uartUuids[1]);
}
else
{
updateStatus("Service discovery failed");
}
}
};
Logcat说:
11-19 10:40:39.363: D/BluetoothAdapter(11717): stopLeScan()
11-19 10:40:39.373: D/BluetoothGatt(11717): connect() - device: DC:6D:75:0C:0F:F9, auto: false
11-19 10:40:39.373: D/BluetoothGatt(11717): registerApp()
11-19 10:40:39.373: D/BluetoothGatt(11717): registerApp() - UUID=3ba20989-5026-4715-add3-a5e31684009a
11-19 10:40:39.373: I/BluetoothGatt(11717): Client registered, waiting for callback
11-19 10:40:49.373: E/BluetoothGatt(11717): Failed to register callback
11-19 10:40:49.533: D/BluetoothGatt(11717): onClientRegistered() - status=0 clientIf=5
11-19 10:40:49.533: E/BluetoothGatt(11717): Bad connection state: 0
11-19 10:40:49.593: D/BluetoothGatt(11717): onClientConnectionState() - status=0 clientIf=5 device=DC:6D:75:0C:0F:F9
11-19 10:40:49.593: W/BluetoothGatt(11717): Unhandled exception: java.lang.NullPointerException
有趣的是,我的外设变为“已连接”状态(我有指示灯),我可以通过演示应用程序或PC BLE加密狗从同一部手机连接到它。任何想法都很感谢。
编辑connectGatt方法返回null,我猜这是预期的。
编辑检查API18源代码时,似乎传递了“注册失败回调”消息,因为registerApp()方法返回false,因为IBluetoothGatt "mService“的registerClient()方法抛出了一个远程异常,可能是在下面的代码行:
enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
因为不会看到下一行中的日志消息。因此,这可能是权限问题,除了应用程序具有蓝牙和bluetooth_admin权限。
发布于 2015-11-20 00:24:56
所以,我的问题是从递归服务运行它。connectGatt与棒棒糖一起工作得很好,但旧版本返回null。在主线程上运行解决了这个问题。这是我的解决方案:
public void connectToDevice( String deviceAddress) {
mDeviceAddress = deviceAddress;
final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(mDeviceAddress);
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
if (device != null) {
mGatt = device.connectGatt(getApplicationContext(), true, mGattCallback);
scanLeDevice(false);// will stop after first device detection
}
}
});
}
发布于 2013-11-20 00:54:38
为了自动连接到蓝牙设备,例如,没有明确的用户输入,我正在尝试这样做,权限BLUETOOTH_PRIVILEDGE是必需的。但是,这对第三方应用程序不可用,因此我的代码失败了。添加一个菜单选项来连接并使用相同的代码可以很好地工作。
http://developer.android.com/reference/android/Manifest.permission.html#BLUETOOTH_PRIVILEGED
https://stackoverflow.com/questions/20069507
复制相似问题