1

私のプログラムは、サーバーとして機能する組み込みのBluetoothデバイスに接続します。近くのBluetoothデバイスを正常に見つけることができましたが、新しいデバイスに接続しようとすると、BluetoothSocket.connect()を呼び出すときにIO例外が原因でプログラムがクラッシュします。何が起こっているのかわからないので、誰か助けてくれたら本当にありがたいです。

ランダムに生成したUUIDと関係があるのではないかと思いますが、完全にはわかりません。

ありがとう。

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    btnScanDevice = (Button) findViewById( R.id.scandevice );

    stateBluetooth = (TextView) findViewById( R.id.bluetoothstate );
    startBluetooth();

    listDevicesFound = (ListView) findViewById( R.id.devicesfound );
    btArrayAdapter = new ArrayAdapter<String>( AndroidBluetooth.this,
            android.R.layout.simple_list_item_1 );
    listDevicesFound.setAdapter( btArrayAdapter );

    CheckBlueToothState();

    btnScanDevice.setOnClickListener( btnScanDeviceOnClickListener );

    registerReceiver( ActionFoundReceiver, new IntentFilter( BluetoothDevice.ACTION_FOUND ) );

    listDevicesFound.setOnItemClickListener( new OnItemClickListener()
    {
      public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) 
      {
          myBtDevice = btDevicesFound.get( arg2 );
          try {
              btSocket = myBtDevice.createRfcommSocketToServiceRecord( MY_UUID );
              iStream = btSocket.getInputStream();
              oStream = btSocket.getOutputStream();
          } catch ( IOException e ) {
              Log.e( "Bluetooth Socket", "Bluetooth not available, or insufficient permissions" );
          } catch ( NullPointerException e ) {
              Log.e( "Bluetooth Socket", "Null Pointer One" );
          }
          myBtAdapter.cancelDiscovery();
          CheckBlueToothState();
          try {
              btSocket.connect();
          } catch ( IOException e ) {
              Log.e( "Bluetooth Socket", "IO Exception" );
          } catch ( NullPointerException e ) {
              Log.e( "Bluetooth Socket", "Null Pointer Two" );
          }
      } 

  });
}

private void CheckBlueToothState() {
    if( myBtAdapter == null ) {
        stateBluetooth.setText("Bluetooth NOT supported" );
    } else {
        if( myBtAdapter.isEnabled() ) {
            if( myBtAdapter.isDiscovering() ) {
                stateBluetooth.setText( "Bluetooth is currently " +
                        "in device discovery process." );
            } else {
                stateBluetooth.setText( "Bluetooth is Enabled." );
                btnScanDevice.setEnabled( true );
            }
        } else {
            stateBluetooth.setText( "Bluetooth is NOT enabled" );
            Intent enableBtIntent = new Intent( BluetoothAdapter.ACTION_REQUEST_ENABLE );
            startActivityForResult( enableBtIntent, REQUEST_ENABLE_BT );
        }
    }
}

private Button.OnClickListener btnScanDeviceOnClickListener = new Button.OnClickListener() {
    public void onClick( View arg0 ) {
        btArrayAdapter.clear();
        myBtAdapter.startDiscovery();
    }
};


@Override
protected void onActivityResult( int requestCode, int resultCode, Intent data ) {
    if( requestCode == REQUEST_ENABLE_BT ) {
        CheckBlueToothState();
    }
}

private final BroadcastReceiver ActionFoundReceiver = new BroadcastReceiver() {
    public void onReceive( Context context, Intent intent ) {
        String action = intent.getAction();
        if( BluetoothDevice.ACTION_FOUND.equals( action ) ) {
            BluetoothDevice btDevice = intent.getParcelableExtra( BluetoothDevice.EXTRA_DEVICE );
            btDevicesFound.add( btDevice );
            btArrayAdapter.add( btDevice.getName() + "\n" + btDevice.getAddress() );
            btArrayAdapter.notifyDataSetChanged();
        }           
    }
};
public static void startBluetooth(){
    try {
        myBtAdapter = BluetoothAdapter.getDefaultAdapter();
        myBtAdapter.enable();
    } catch ( NullPointerException ex ) {
        Log.e( "Bluetooth", "Device not available" );
    }
}

public static void stopBluetooth() {
    myBtAdapter.disable();
}
4

1 に答える 1

4

投稿したコードには2つの問題がありますが、クラッシュに関連しているのは1つだけです。

logcatでのクラッシュは、「コマンドが拒否されました」のようなものである可能性があります。UUIDは、組み込みデバイスで公開されているサービスを指す必要がある値であり、ランダムに生成することはできません。つまり、アクセスするRFCOMM SPP接続には、そのサービスを識別するために公開する特定のUUIDがあり、ソケットを作成するときに、一致するUUIDを使用する必要があります。

私が書いたこのブログ投稿は、プログラムに挿入する必要のある適切なUUIDを取得するためにデバイスにクエリを実行する方法を支援する可能性があります。Android 4.0より前のSDKは、事前に知っていることを前提としていたため(Bluetooth OEMなどから入手した)、デバイスから少し回り道をして発見しました。幸運にも4.0.3デバイスがfetchUuidsWithSdp()あり、getUuids()パブリックメソッドになっている場合は、それらを直接呼び出して、公開されているすべてのサービスとそれに関連するUUID値を見つけることができます。

後で発生する可能性のあるコードの2番目の問題は、接続するまでソケットからデータストリームを取得できないことです。そのため、メソッドを次のように書き直す必要があります。

      myBtDevice = btDevicesFound.get( arg2 );
      try {
          btSocket = myBtDevice.createRfcommSocketToServiceRecord( MY_UUID );
      } catch ( IOException e ) {
          Log.e( "Bluetooth Socket", "Bluetooth not available, or insufficient permissions" );
      }

      myBtAdapter.cancelDiscovery();
      CheckBlueToothState();
      try {
          btSocket.connect();
          //Get streams after connect() returns without error
          iStream = btSocket.getInputStream();
          oStream = btSocket.getOutputStream();
      } catch ( IOException e ) {
          Log.e( "Bluetooth Socket", "IO Exception" );
      } catch ( NullPointerException e ) {
          Log.e( "Bluetooth Socket", "Null Pointer Two" );
      }

HTH

于 2012-06-12T14:58:46.587 に答える