[英]Connecting to Embedded Bluetooth Device From Android Device
我刚刚开始通过我的Android手机连接到嵌入式BT设备。它连接正常,但我正面临问题,当我没有正确断开连接。 我的意思是先关闭套接字,然后打开任何i / o流。
但是当我在设备中突然关闭我的蓝牙时,我怎么知道蓝牙会断开连接。 是否有任何方法可以在APP中随时接收蓝牙断开监听器。
有任何想法吗 ....? 谢谢
mmSocket= device.createRfcommSocketToServiceRecord(uuidToTry);
try
{
mmSocket.connect();
}
catch (IOException e)
{
mmSocket.close();
}
你可以这样做 - 在你的代码中创建一个广播接收器像这样:
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (!mBluetoothAdapter.isEnabled()) {
// BT is turened off.
}
else{
// BT is turened on.
}
}
}
};
并注册该broadcastreceiver以用于以下intent过滤器:
IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED); registerReceiver(mReceiver, filter);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.