2016-04-28 135 views
1

我找到了用于管理服务连接的示例代码。但我不知道如何使用它。在Android服务上连接蓝牙

我只写在这里,我不明白的代码,看到所有的代码:

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()要执行时如何通过我发现的蓝牙设备ID?

当我没有执行ACTION_FOUND时,它试图连接哪个蓝牙设备?

回答

0

可能有点晚,但以防万一。如果我记得正确,有许多方法可以连接到设备,因为它的mac地址。

  • 将设备的地址作为额外的创建意图传递。

    在UI活动

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

    在蓝牙服务

    public int onStartCommand(Intent intent, int flags, int startId) { 
        String address = intent.getExtras("DEVICE_ADDRESS"); 
    
        ###DO SOMETHING WITH ADDRESS HERE### 
    } 
    
  • 创建您服务 'connectToDevice(字符串地址)' 的方法,并在任何地方你都叫它您的BroadcastReceiver。

    在蓝牙服务

    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()); 
    
    } 
    

你应该检查出由Android提供的BluetoothChat example。我认为这显示了你需要的很多东西。