2013-02-14 117 views
0

我掃描設備,我想連接到另一個設備「HC-07」,如果它是在範圍內或啓用我的Android設備。我嘗試了一些,但沒有連接...任何幫助嗎?我用uuid串行連接另一個藍牙設備...發現後我如何連接到藍牙設備?

private final BroadcastReceiver ActionFoundReceiver = new BroadcastReceiver(){ 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     // TODO Auto-generated method stub 
     String action = intent.getAction(); 
     if(BluetoothDevice.ACTION_FOUND.equals(action)) { 
      device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 

      short rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, Short.MIN_VALUE);//find rssi dBm 

      int pair_state = device.getBondState(); 

      if(device.getName()=="HC-07"){ 
       try { 
       openBT(); 
       Toast.makeText(getBaseContext(),"HC-07 found", Toast.LENGTH_SHORT).show(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
        } 
       } 

      if (pair_state == 12){ pair_str="Paired"; 

      btArrayAdapter.add("Dev name: "+ device.getName() + "\n" + "Mac: " + device.getAddress() + "\n" + "Signal RSSI: " + rssi + "dBm" + "\n"+pair_str); 

      } 

      btArrayAdapter.notifyDataSetChanged(); 

     } 
    }}; 

    void openBT() throws IOException 
    { 
     UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); 
     mmSocket = device.createRfcommSocketToServiceRecord(uuid);  
     mmSocket.connect(); 
     mmOutputStream = mmSocket.getOutputStream(); 
    } 

回答

0

你仔細看了一下提供的BluetoothChat示例嗎?這將向您展示如何掃描並連接到藍牙設備以及如何使用API​​來完成此操作。 http://developer.android.com/resources/samples/BluetoothChat/index.html

檢查這方面的例子:

Bluetooth Basics

http://developer.android.com/resources/samples/BluetoothChat/index.html

Code samples for Android Bluetooth programming

http://marakana.com/forums/android/examples/50.html

+0

我readed,但很複雜,我....簡單容易一些代碼辦法? – 2013-02-14 09:21:10

+0

我想要自動連接,如果我的設備是列表... – 2013-02-14 09:23:09

+0

我編輯我的答案 – jlopez 2013-02-14 09:23:09