首先,对不起我的英语,第二,我在Android工作室有一个BluetoothGattCallback的问题,应用程序运行,我进入研究和连接,它没关系,但回调中的方法永远不会启动,我的校长兴趣是rssi。
这是我的gattcallback:
private final BluetoothGattCallback mGattCallback =
new BluetoothGattCallback() {
@Override
public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
Toast.makeText(MainActivity.this, "fin qui tutto ok", Toast.LENGTH_LONG).show();
TextView Rssitext = (TextView) findViewById(R.id.textView8);
Rssitext.setText("RSSI"+ "null");
if (status == gatt.STATE_CONNECTED) {
Toast.makeText(MainActivity.this,rssi, Toast.LENGTH_LONG).show();
Rssitext.setText("RSSI"+ rssi);
}
else {
Toast.makeText(MainActivity.this,"fail read rssi", Toast.LENGTH_LONG).show();
Rssitext.setText("RSSI"+ "null");
}
}
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status,
int newState) {
String intentAction;
if (newState == BluetoothProfile.STATE_CONNECTED) {
intentAction = ACTION_GATT_CONNECTED;
mConnectionState = STATE_CONNECTED;
boolean rssiStatus = mBluetoothGatt.readRemoteRssi();
broadcastUpdate(intentAction);
Toast.makeText(MainActivity.this, "Connected to GATT server." , Toast.LENGTH_LONG).show();
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
intentAction = ACTION_GATT_DISCONNECTED;
mConnectionState = STATE_DISCONNECTED;
Toast.makeText(MainActivity.this,"Disconnected from GATT server.", Toast.LENGTH_LONG).show();
broadcastUpdate(intentAction);
}
}
@Override
// New services discovered
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
} else {
Toast.makeText(MainActivity.this,"onServicesDiscovered received: " + status, Toast.LENGTH_LONG).show();
}
}
现在,当我打电话时,我会使用has来自动连接到特定设备
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// Add the name and address to an array adapter to show in a ListView
Toast.makeText(MainActivity.this, device.getName() + " " + device.hashCode(), Toast.LENGTH_LONG).show();
if ((device.hashCode() == 676210690)|| (device.hashCode() == -1001190065))
{
mBluetoothGatt= device.connectGatt(context, false, mGattCallback);
Toast.makeText(MainActivity.this,
"sought after device found", Toast.LENGTH_LONG).show();
mBluetoothGatt.readRemoteRssi();
}
}
}
};