我在Moto X上使用的是Android 4.4.2。我有一个自定义蓝牙低功耗板,当没有设备连接时,它会打开红色LED。它一次只支持一个连接的设备。我正在构建一个Android应用程序,它试图连接到这个BLE设备并写入其特性,然后断开它。我有两个问题:在为特征写一个值后,我正在做:
mBluetoothGatt.disconnect();
mBluetoothGatt.close();
mBluetoothGatt = null;
mBluetoothAdapter.cancelDiscovery();
broadcastUpdate(ACTION_GATT_DISCONNECTED);
但实际上并没有断开我的智能手机(BLE设备上的LED保持关闭状态,这意味着某些设备仍然连接到它)。但是,当我关闭智能手机上的蓝牙适配器时,LED会立即亮起。
第二个问题是我无法从第一次尝试中写出我的特征,我必须第二次调用相同的序列。
以下是调用我的字符编写的过程:
public void trytounlock() {
Context context = getApplicationContext();
if (connect("E2:0C:B9:4D:B9:56"))
{
Log.w(TAG, "connect returned true!");
}
else {
Log.w(TAG, "connect returned false - warum?");
};
if (mBluetoothAdapter == null ) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
else
{
Log.w(TAG, "BluetoothAdapter initialized successfully!");
}
if ( mBluetoothGatt == null) {
Log.w(TAG, "GATT not initialized");
return;
}
else
{
Log.w(TAG, "GATT initialized succesfully!");
}
/*check if the service is available on the device*/
BluetoothGattService mCustomService = mBluetoothGatt.getService(UUID.fromString("0000f00d-1212-efde-1523-785fef13d123"));
while (!mBluetoothGatt.discoverServices())
{
Log.w(TAG, "Waiting for service discovery");
}
int duration = Toast.LENGTH_SHORT;
if(mCustomService == null){
Log.w(TAG, "Custom BLE Service not found here. ");
return;
}
else
{
Log.w(TAG, "Custom BLE Service found successfully!");
}
BluetoothGattCharacteristic mWriteCharacteristic = mCustomService.getCharacteristic(UUID.fromString("0000beef-1212-efde-1523-785fef13d123"));
byte[] raw={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
mWriteCharacteristic.setValue(raw);
if(mBluetoothGatt.writeCharacteristic(mWriteCharacteristic) == false){
Log.w(TAG, "Failed to write characteristic");
Toast.makeText(context, "Failed to write characteristic - ошибочка!!", duration).show();
}
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized, while trying to disconnect - weird!!!");
}
else {
mBluetoothGatt.disconnect();
}
mWriteCharacteristic = null;
mCustomService = null;
if (mBluetoothGatt == null) {
}
else {
mBluetoothGatt.disconnect();
mBluetoothGatt.close();
mBluetoothGatt = null;
mBluetoothAdapter.cancelDiscovery();
}
broadcastUpdate(ACTION_GATT_DISCONNECTED);
}
这是我的连接程序:
public boolean connect(final String address) {
if (mBluetoothAdapter == null || address == null) {
Log.w(TAG, "BluetoothAdapter not initialized or unspecified address.");
return false;
}
final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
if (device == null) {
Log.w(TAG, "Device not found. Unable to connect.");
return false;
}
// We want to directly connect to the device, so we are setting the autoConnect
// parameter to false.
mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
Log.d(TAG, "Trying to create a new connection.");
mBluetoothDeviceAddress = address;
mConnectionState = STATE_CONNECTING;
return true;
}
// Implements callback methods for GATT events that the app cares about. For example,
// connection change and services discovered.
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
String intentAction;
if (newState == BluetoothProfile.STATE_CONNECTED) {
intentAction = ACTION_GATT_CONNECTED;
mConnectionState = STATE_CONNECTED;
broadcastUpdate(intentAction);
Log.i(TAG, "Connected to GATT server.");
// Attempts to discover services after successful connection.
Log.i(TAG, "Attempting to start service discovery:" +
mBluetoothGatt.discoverServices());
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
intentAction = ACTION_GATT_DISCONNECTED;
mConnectionState = STATE_DISCONNECTED;
Log.i(TAG, "Disconnected from GATT server.");
broadcastUpdate(intentAction);
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
} else {
Log.w(TAG, "onServicesDiscovered received: " + status);
}
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic,
int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
}
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic) {
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
}
};
private void broadcastUpdate(final String action) {
final Intent intent = new Intent(action);
sendBroadcast(intent);
}
所以,问题是: