我正在使用CC2640和我的自定义APP。我有几天无法解决的问题。实际上,当我使用配对时,我不会冒将BLE设备连接到APP的风险。实际上,在输入密码后的第一个连接处,它告诉我它已连接但不起作用,然后第二个连接不再读取“ CONNECTED”日志。有人能帮我吗?这就是我写的
MainActivity
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { ///here??
super.onConnectionStateChange(gatt, status, newState);
String intentAction;
BluetoothAdapter bleAdapter = ((BluetoothManager) getSystemService(BLUETOOTH_SERVICE)).getAdapter();
Set<BluetoothDevice> paired = bleAdapter.getBondedDevices();
for (BluetoothDevice device : paired) {
if (device.getName().equals("RCQ4-BLE")) {
Log.i("Paired", "It paired" + bleAdapter.getBondedDevices());
if (newState == BluetoothProfile.STATE_CONNECTED) {
intentAction = ACTION_GATT_CONNECTED;
Log.i("gattCallBack", "STATE CONNECTED");
broadcastUpdate(intentAction);
Log.i(TAG, "Connected to GATT server");
Log.i(TAG, "Attempting to start service discovery:" + gatt.discoverServices());
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
intentAction = ACTION_GATT_DISCONNECTED;
Log.i("gattCallBack", "STATE DISCONNECTED");
broadcastUpdate(intentAction);
} else if (mBluetoothGatt != null) {
mBluetoothGatt.disconnect();
mBluetoothGatt = null;
} else {
Log.w(TAG, "onConnectionStateChange() status = " + status + "newState = " + newState);
}
}
}
}
Selected Device
private final BroadcastReceiver mBroadcastReceiver4 = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, -1);
int prevstate = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, -1);
String msg = "Bond state change: state" + pairstate(state) + "previous state" + pairstate(prevstate);
Log.w("Bond state receiver", msg);
}
private String pairstate(int state) {
switch (state) {
case BluetoothDevice.BOND_BONDING: Log.i("Bond status:", "Bonding..");
//device.createBond();
// onBondingRequired();
break;
case BluetoothDevice.BOND_BONDED:
Log.i("Bond status:", "Bonded");
// onBonded();
break;
case BluetoothDevice.BOND_NONE:
Log.i("Bond status:", "Fail");
// onBondingFailed();
default:
return String.valueOf(state);
}
return(null);
}
};
private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
switch(action)
{
case MainActivity.ACTION_GATT_CONNECTED:
// updateConnectionState(R.string.connected);
invalidateOptionsMenu();
break;
case MainActivity.ACTION_GATT_DISCONNECTED:
// updateConnectionState(R.string.disconnected);
invalidateOptionsMenu();
clearUI();
break;
case MainActivity.ACTION_GATT_SERVICES_DISCOVERED:
initializeGattServiceUIElements(mMainActivity.getSupportedGattServices());
break;