我目前正在开发一个应用程序,用于连接一个特殊的主板,其软件由另一家公司使用经典蓝牙和蓝牙LE开发(当然不是同时)。使用的Panasonic PAN1026可以实现可靠的经典蓝牙SPP连接。
问题是,尽管可以成功建立与GATT服务器的连接,但在使用蓝牙LE时我无法执行服务发现。即使在十分钟之后,服务发现也不会返回任何结果,如下面的日志所示:
02-04 20:06:39.137 31757-31757/... D/BluetoothGatt﹕ connect() - device: 26:0B:D9:55:55:55, auto: false
02-04 20:06:39.137 31757-31757/... D/BluetoothGatt﹕ registerApp()
02-04 20:06:39.137 31757-31757/... D/BluetoothGatt﹕ registerApp() - UUID=e4b1b4b7-52d6-433a-9071-88fd5d6bf556
02-04 20:06:39.137 31757-31757/... I/BluetoothGatt﹕ Client registered, waiting for callback
02-04 20:06:39.137 31757-31769/... D/BluetoothGatt﹕ onClientRegistered() - status=0 clientIf=5
02-04 20:06:40.047 31757-31768/... D/BluetoothGatt﹕ onClientConnectionState() - status=133 clientIf=5 device=26:0B:D9:55:55:55
02-04 20:06:40.047 31757-31768/... I/bluetoothle.GATTHandler﹕ Bluetooth LE connection established!
02-04 20:06:40.047 31757-31768/... D/BluetoothGatt﹕ discoverServices() - device: 26:0B:D9:55:55:55
02-04 20:06:40.047 31757-31768/... I/bluetoothle.BluetoothLEConnection﹕ Starting service discovery: true
这是我的代码:
this.gattHandler = new GATTHandler(this);
// run the service discovery on the UI thread for being compatible with Samsung mobile phones
if (this.getDisplayFragment().getActivity() != null)
{
this.getDisplayFragment().getActivity().runOnUiThread(
new Runnable()
{
@Override
public void run()
{
gattConnection = getDeviceToConnectTo().connectGatt(getDisplayFragment().getActivity().getApplicationContext(), false, gattHandler);
}
});
}
public class GATTHandler extends BluetoothGattCallback
{
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState)
{
if (newState == BluetoothProfile.STATE_CONNECTED)
{
this.setGATTConnectionState(GATTHandler.STATE_CONNECTED);
this.getEventNotifier().onConnectionEstablished();
Log.i(GATTHandler.TAG, "Bluetooth LE connection established!");
}
else if (newState == BluetoothProfile.STATE_DISCONNECTED)
{
this.setGATTConnectionState(GATTHandler.STATE_DISCONNECTED);
this.getEventNotifier().onDisconnected();
Log.i(GATTHandler.TAG, "Bluetooth LE connection closed!");
}
else if (newState == BluetoothProfile.STATE_CONNECTING)
{
this.setGATTConnectionState(GATTHandler.STATE_CONNECTING);
Log.i(GATTHandler.TAG, "Establishing a Bluetooth LE connection!");
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status)
{
if (status == BluetoothGatt.GATT_SUCCESS)
{
Log.i(GATTHandler.TAG, "Discovered some services!");
this.getEventNotifier().onServicesDiscovered();
}
else
{
Log.wtf(GATTHandler.TAG, "Service discovery status: " + String.valueOf(status));
}
}
}
但是,在Ubuntu 14.04 x64上使用gattool时,服务发现可以正常工作,如下面的输出所示:
hcitool lescan
26:0B:D9:55:55:55 Spp
gatttool -I -b 26:0B:D9:55:55:55
[ ][26:0B:D9:55:55:55][LE]> connect
[CON][26:0B:D9:55:55:55][LE]> primary
[CON][26:0B:D9:55:55:55][LE]>
attr handle: 0x0001, end grp handle: 0x0021 uuid: 00001800-0000-1000-8000-00805f9b34fb
attr handle: 0x0100, end grp handle: 0x0131 uuid: 0000180a-0000-1000-8000-00805f9b34fb
attr handle: 0x0200, end grp handle: 0x0212 uuid: e079c6a0-aa8b-11e3-a903-0002a5d5c51b
[CON][26:0B:D9:55:55:55][LE]> disconnect
[ ][26:0B:D9:55:55:55][LE]>
我正在使用三星Galaxy S3 Neo和原版Android 4.4.2 ROM进行开发。我已经阅读了以下两个问题,因此我认为Android的BlueDroid有点儿错误:
提前感谢您的回答!
的Lukas