Hus*_*ain 15 android bluetooth
我目前正在开发一个Android应用程序..我必须在应用程序当前正在运行时关闭设备的蓝牙时通知用户..如何通知远程设备tat BT已关闭?提前致谢
ern*_*azm 25
使用intent动作注册BroadcastReceiverBluetoothAdapter.ACTION_STATE_CHANGED并将notifiyng代码移动到onReceive方法中.不要忘记检查新状态是否为OFF
if(BluetoothAdapter.ACTION_STATE_CHANGED.equals(intent.getAction())) {
if(intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1)
== BluetoothAdapter.STATE_OFF)
// Bluetooth was disconnected
}
Run Code Online (Sandbox Code Playgroud)
Vin*_*uza 11
如果要检测用户何时断开其蓝牙连接,以及稍后检测用户何时将蓝牙断开连接,则应执行以下步骤:
1)获取用户BluetoothAdapter:
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
Run Code Online (Sandbox Code Playgroud)
2)创建和配置您的Receiver,代码如下:
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// It means the user has changed his bluetooth state.
if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
if (btAdapter.getState() == BluetoothAdapter.STATE_TURNING_OFF) {
// The user bluetooth is turning off yet, but it is not disabled yet.
return;
}
if (btAdapter.getState() == BluetoothAdapter.STATE_OFF) {
// The user bluetooth is already disabled.
return;
}
}
}
};
Run Code Online (Sandbox Code Playgroud)
3)将您的BroadcastReceiver注册到您的活动中:
this.registerReceiver(mReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9498 次 |
| 最近记录: |