[英]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.