2016-04-28 30 views
1

サービスの接続を管理するためのサンプルコードが見つかりました。しかし、私はどのようにそれを使用するのか分からない。 、私はサービスなしブルートゥースするcorrecly接続AndroidサービスでBluetoothを接続

Link Here

CODE

public int onStartCommand(Intent intent, int flags, int startId) { 
    Log.d("popopo", "Onstart Command"); 
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
    if (mBluetoothAdapter != null) { 
     device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
     deviceName = device.getName(); 
     String macAddress = device.getAddress(); 
     if (macAddress != null && macAddress.length() > 0) { 
      connectToDevice(macAddress); 
     } else { 
      stopSelf(); 
      return 0; 
     } 
    } 
    String stopservice = intent.getStringExtra("stopservice"); 
    if (stopservice != null && stopservice.length() > 0) { 
     stop(); 
    } 
    return START_STICKY; 
} 

私はすべてのコードを見て、ここに私は理解していないコードを書きますこのコードで端末を取得する:

if (BluetoothDevice.ACTION_FOUND.equals(action)) { 
    // Get the BluetoothDevice object from the Intent 
    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
    // Add the name and address to an array adapter to show in a ListView 
    mArrayAdapter.add(device.getName() + "\n" + device.getAddress()); 
} 

私の質問は:onStartCommand()が実行されるときに、私のBluetoothデバイスIDを渡す方法は?

ACTION_FOUNDを実行していないときに接続しようとしているBluetoothデバイスはどれですか?

答えて

0

ちょっと遅れるかもしれませんが、ちょっとでもいいかもしれません。私が正しくリコールすれば、MACアドレスが与えられているので、デバイスに接続するにはいくつかの方法があります。

  • 作成の意図でデバイスのアドレスを余分に渡します。

    UI活動に

    Intent intent=new Intent(this,BluetoothService.class)); 
    intent.putExtra("DEVICE_ADDRESS",device.getAddress()); 
    
    startService(intent); 
    

    Bluetoothサービスで

    public int onStartCommand(Intent intent, int flags, int startId) { 
        String address = intent.getExtras("DEVICE_ADDRESS"); 
    
        ###DO SOMETHING WITH ADDRESS HERE### 
    } 
    
  • あなたのサービスに 'connectToDevice(文字列のアドレス)' メソッドを作成し、あなたが持っているどこからそれを呼び出しますあなたのBroadcastReceiver。放送受信機でBluetoothサービス

    protected synchronized void connectToDevice(BluetoothDevice device){//Cancells any threads attempting to connect and starts a new connectThread (new connection) 
    
        if (mConnectThread != null) { 
         mConnectThread.cancel(); 
         mConnectThread = null; 
         Log.d(TAG,"Cancelled connectThread"); 
        } 
    
    
        if (mConnectedThread != null) { 
         mConnectedThread.cancel(); 
         mConnectedThread = null; 
         Log.d(TAG,"Cancelled connectedThread"); 
    
        } 
    
        mConnectThread = new ConnectThread(device); 
        mConnectThread.start(); 
        Log.d(TAG,"Starting connectThread"); 
    } 
    

    if (BluetoothDevice.ACTION_FOUND.equals(action)) { 
        // Get the BluetoothDevice object from the Intent 
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
        // Add the name and address to an array adapter to show in a ListView 
        mArrayAdapter.add(device.getName() + "\n" + device.getAddress()); 
        BluetoothService mBluetoothService = new BluetoothService(); 
        mBluetoothService.connectToDevice(device.getAddress()); 
    
    } 
    

あなたはアンドロイドが提供するBluetoothChat exampleをチェックアウトする必要があります。私はそれがあなたが必要とする多くの声を示すと思います。

関連する問題