2014年5月28日水曜日

Bluetoothで通信を行う

Bluetoothで通信を行うって記事を参考にbluetooth通信を行うアプリを作ろうと思ってるんだけど、
さっきからぜんぜんエラーが消えなくて何かと思ってた

元記事Bluetoothで通信を行う(2)

ACTION_DISCOVERY_STARTED
ACTION_DISCOVERY_FINISHED

の2つは、頭にBluetoothAdapter.  ってつける必要があって、

ACTION_FOUND
ACTION_NAME_CHANGED

の2つは、頭にBluetoothDevice.   ってつける必要があった。


だから、その部分のコードは、
private final BroadcastReceiver DevieFoundReceiver = new BroadcastReceiver(){
   //検出されたデバイスからのブロードキャストを受ける
   @Override
   public void onReceive(Context context, Intent intent){
       String action = intent.getAction();
       String dName = null;
       BluetoothDevice foundDevice;
       ListView nonpairedList = (ListView)findViewById(R.id.listview);
       if(BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)){
        Log.d("ACTION_DISCOVERY_STARTED","スキャン開始");
       }
       if(BluetoothDevice.ACTION_FOUND.equals(action)){
           //デバイスが検出された
           foundDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
           if((dName = foundDevice.getName()) != null){
               if(foundDevice.getBondState() != BluetoothDevice.BOND_BONDED){
                   //接続したことのないデバイスのみアダプタに詰める
                connectableDeviceAdapter.add(dName + "\n" + foundDevice.getAddress());
                   Log.d("ACTION_FOUND", dName);
               }
           }
           nonpairedList.setAdapter(connectableDeviceAdapter);
       }
       if(BluetoothDevice.ACTION_NAME_CHANGED.equals(action)){
           //名前が検出された
           Log.d("ACTION_NAME_CHANGED", dName);
           foundDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
           if(foundDevice.getBondState() != BluetoothDevice.BOND_BONDED){
               //接続したことのないデバイスのみアダプタに詰める
               connectableDeviceAdapter.add(dName + "\n" + foundDevice.getAddress());
           }
           nonpairedList.setAdapter(connectableDeviceAdapter);
       }

       if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
           Log.d("ACTION_DISCOVERY?FINISHED","スキャン終了");
       }
   }

};

ってなる。

本来はならないのかな?
よくわかりません。

分かる人教えてください。
おわり