7

Androidフォンを介した組み込みBTデバイスへの接続に取り組み始めました。接続は正常ですが、正しく切断しないと問題が発生します。つまり、最初にソケットを閉じてから、i/oスチームを開きます。

しかし、デバイスで突然Bluetoothをオフにすると、Bluetoothが切断されていることがわかります。APPでBluetooth切断リスナーを常に受信する方法はありますか?

何か案は ....?ありがとう

mmSocket= device.createRfcommSocketToServiceRecord(uuidToTry);
try
{
    mmSocket.connect();
}
catch (IOException e)
{
    mmSocket.close();
}
4

1 に答える 1

1

このようにすることができます- コードでブロードキャストレシーバーを作成する このように:

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.
              }
        }
 }
};

そのブロードキャストレシーバーを次のインテント フィルターに登録します。

IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED); registerReceiver(mReceiver, filter);

于 2013-02-27T10:19:57.700 に答える