如何检测我的 android 设备何时从配对的蓝牙设备获得 connected/disconnected
How to detect whenever my android device gets connected/disconnected from paired bluetooth device
我想收到一个事件,用于检测蓝牙在我的设备中何时配对或取消配对。
一开始我找到了http://developer.android.com/reference/android/bluetooth/BluetoothServerSocket.html。
我关注了这个博客:http://android-er.blogspot.co.il/2014/12/make-bluetooth-connection-between.html 这非常有用,但是,当建立新连接时没有检测到。
不用说,我对蓝牙实现毫无用处,这一点我很清楚:
final BluetoothAdapter.LeScanCallback bleScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, final int rssi, byte[] scanRecord) { ... }
}
任何帮助将不胜感激。
我想我应该找到一种方法来访问其中一个 类:
BluetoothBondStateMachine、BTConnectionReceiver、BluetoothBondStateMachine
这将是一个不错的开始,因为我可以在 logcat 中看到每当创建配对/取消配对时,那些 类 就会检测到它。
已连接到蓝牙设备 "shine" :
07-28 18:05:49.284 8332-8332/? I/BTConnectionReceiver﹕ onReceive(context, Intent { act=android.bluetooth.device.action.ACL_CONNECTED flg=0x4000010 cmp=com.google.android.googlequicksearchbox/com.google.android.search.core.service.BluetoothConnectionReceiver (has extras) }, [BluetoothDevice: address=F2:3F:FB:30:A1:75, alias=null, name=Shine, majorDeviceClass=7936, deviceClass=7936]
07-28 18:05:49.285 8332-8332/? I/BluetoothClassifier﹕ Bluetooth Device Name: Shine
07-28 18:05:49.830 19769-19793/? W/bt_smp﹕ smp_send_id_info
07-28 18:05:49.836 19769-19786/? I/BluetoothBondStateMachine﹕ bondStateChangeCallback: Status: 0 Address: F2:3F:FB:30:A1:75 newState: 2
07-28 18:05:49.838 19769-19787/? I/BluetoothBondStateMachine﹕ Bond State Change Intent:F2:3F:FB:30:A1:75 OldState: 11 NewState: 12
07-28 18:05:49.838 19769-19787/? I/BluetoothBondStateMachine﹕ StableState(): Entering Off State
07-28 18:05:50.564 19769-19793/? E/bt_att﹕ gatt_disc_cmpl_cback() - Register for service changed indication failure
07-28 18:05:51.189 19769-19793/? W/bt_bta_gattc﹕ bta_gattc_explore_srvc no more services found
07-28 18:05:51.191 19769-19793/? I/bt_bta_dm﹕ bta_dm_gatt_disc_result service_id_uuid_len=2
所以,最后,在收听了蓝牙必须提供的所有事件之后,我想我只需要收听 BluetoothDevice.ACTION_BOND_STATE_CHANGED
并从发送的意图中检测数据。如果有人被困在这个问题上,我希望它能帮助你。
我的完整代码:
public class MainActivity extends Activity {
private TextView mTextLog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextLog = (TextView) findViewById(R.id.text_log);
registerReceiver(mReceiver, new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED));
}
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(mReceiver);
}
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE,
BluetoothDevice.ERROR);
if (state == BluetoothDevice.BOND_BONDED) {
attachText("Device " + device + " PAIRED");
} else if (state == BluetoothDevice.BOND_BONDING) {
attachText("Device " + device + " pairing is in process...");
} else if (state == BluetoothDevice.BOND_NONE) {
attachText("Device " + device + " is unpaired");
} else {
attachText("Device " + device + " is in undefined state");
}
}
};
private void attachText(String text) {
String currentText = mTextLog.getText() == null ? "" : mTextLog.getText().toString();
mTextLog.setText(currentText + "\n" + text);
}
}
我想收到一个事件,用于检测蓝牙在我的设备中何时配对或取消配对。
一开始我找到了http://developer.android.com/reference/android/bluetooth/BluetoothServerSocket.html。
我关注了这个博客:http://android-er.blogspot.co.il/2014/12/make-bluetooth-connection-between.html 这非常有用,但是,当建立新连接时没有检测到。不用说,我对蓝牙实现毫无用处,这一点我很清楚:
final BluetoothAdapter.LeScanCallback bleScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, final int rssi, byte[] scanRecord) { ... }
}
任何帮助将不胜感激。
我想我应该找到一种方法来访问其中一个 类: BluetoothBondStateMachine、BTConnectionReceiver、BluetoothBondStateMachine 这将是一个不错的开始,因为我可以在 logcat 中看到每当创建配对/取消配对时,那些 类 就会检测到它。
已连接到蓝牙设备 "shine" :07-28 18:05:49.284 8332-8332/? I/BTConnectionReceiver﹕ onReceive(context, Intent { act=android.bluetooth.device.action.ACL_CONNECTED flg=0x4000010 cmp=com.google.android.googlequicksearchbox/com.google.android.search.core.service.BluetoothConnectionReceiver (has extras) }, [BluetoothDevice: address=F2:3F:FB:30:A1:75, alias=null, name=Shine, majorDeviceClass=7936, deviceClass=7936]
07-28 18:05:49.285 8332-8332/? I/BluetoothClassifier﹕ Bluetooth Device Name: Shine
07-28 18:05:49.830 19769-19793/? W/bt_smp﹕ smp_send_id_info
07-28 18:05:49.836 19769-19786/? I/BluetoothBondStateMachine﹕ bondStateChangeCallback: Status: 0 Address: F2:3F:FB:30:A1:75 newState: 2
07-28 18:05:49.838 19769-19787/? I/BluetoothBondStateMachine﹕ Bond State Change Intent:F2:3F:FB:30:A1:75 OldState: 11 NewState: 12
07-28 18:05:49.838 19769-19787/? I/BluetoothBondStateMachine﹕ StableState(): Entering Off State
07-28 18:05:50.564 19769-19793/? E/bt_att﹕ gatt_disc_cmpl_cback() - Register for service changed indication failure
07-28 18:05:51.189 19769-19793/? W/bt_bta_gattc﹕ bta_gattc_explore_srvc no more services found
07-28 18:05:51.191 19769-19793/? I/bt_bta_dm﹕ bta_dm_gatt_disc_result service_id_uuid_len=2
所以,最后,在收听了蓝牙必须提供的所有事件之后,我想我只需要收听 BluetoothDevice.ACTION_BOND_STATE_CHANGED
并从发送的意图中检测数据。如果有人被困在这个问题上,我希望它能帮助你。
我的完整代码:
public class MainActivity extends Activity {
private TextView mTextLog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextLog = (TextView) findViewById(R.id.text_log);
registerReceiver(mReceiver, new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED));
}
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(mReceiver);
}
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE,
BluetoothDevice.ERROR);
if (state == BluetoothDevice.BOND_BONDED) {
attachText("Device " + device + " PAIRED");
} else if (state == BluetoothDevice.BOND_BONDING) {
attachText("Device " + device + " pairing is in process...");
} else if (state == BluetoothDevice.BOND_NONE) {
attachText("Device " + device + " is unpaired");
} else {
attachText("Device " + device + " is in undefined state");
}
}
};
private void attachText(String text) {
String currentText = mTextLog.getText() == null ? "" : mTextLog.getText().toString();
mTextLog.setText(currentText + "\n" + text);
}
}