2013-10-11 57 views
5

我在android中通過藍牙發送圖像,並且想要獲取圖像發送到的設備的MAC地址。如何獲得藍牙連接設備的MAC地址在android中

請在下面找到我的代碼。

private void bluetoothadd(){ 
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
    if (mBluetoothAdapter == null) { 
     // Device does not support Bluetooth 

     Log.e("Bluetooth ","not found"); 
    } 

    if (!mBluetoothAdapter.isEnabled()) { 
     Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
     startActivity(enableBtIntent); 

     Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); 
     // If there are paired devices 
     if (pairedDevices.size() > 0) { 
      // Loop through paired devices 
      for (BluetoothDevice device : pairedDevices) { 


       Log.e("Mac Addressess","are: "+mBluetoothAdapter.getRemoteDevice(device.getAddress())); 
      } 
      } 
     } 

} 

我得到所有配對設備的MAC地址。我只想要一個設備的MAC地址是傳輸數據的。

+1

您要查找的地址,當其它設備連接到用戶的設備,或建立(恢復階段)的連接,甚至之前.. ??? – Shiva

+0

我想要一個其他設備連接時的地址,我可以得到數據發送到哪個設備的MAC地址。 –

+0

檢查以下鏈接 http://stackoverflow.com/questions/10795424/how-to-get-the-bluetooth-devices-as-a-list http://stackoverflow.com/questions/16471204/discovering-bluetooth-devices-listview-will-not-get-updated –

回答

-3

試試吧。

WifiManager manager = (WifiManager) getSystemService(Context.WIFI_SERVICE); 
WifiInfo info = manager.getConnectionInfo(); 
String address = info.getMacAddress(); 
+0

不,這是wifi。雖然術語MAC地址的使用通常與wifi關聯,但它也可以用於藍牙BD_ADDR。 – Tom

+0

Mac地址僅適用於Wi-Fi模塊! – alezhka

0

所以,它聽起來像你想要得到你有連接的設備的bd_addr/mac?然後請注意,BluetoothSocket類有一個成員'getRemoteDevice',它返回一個BluetoothDevice實例,代表您連接的設備,您可以在其上調用getAddress()來獲取MAC。

或者您可以註冊ACTION_ACL_CONNECTED,其中包含'EXTRA_DEVICE',它將引導您進入BluetoothDevice。

1

當意圖被激活以連接到遠程設備並且設備成功建立時,設備地址作爲額外數據返回,標誌爲EXTRA_DEVICE_ADDRESS

您可以檢查連接,並建立它

if (!mBluetoothAdapter.isEnabled()) { 
      Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
      startActivityForResult(enableIntent, REQUEST_ENABLE_BT); 

您可以檢查onActivityResult功能的活動來找到這樣

public void onActivityResult(int requestCode, int resultCode, Intent data) { 

     switch (requestCode) { 
     case REQUEST_CONNECT_DEVICE: 
      // When DeviceListActivity returns with a device to connect 
      if (resultCode == Activity.RESULT_OK) { 
       // Get the device MAC address 
       String add = data.getExtras() 
            .getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS); 
       address= add.toString(); 

       // Get the BluetoothDevice object 
       BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address); 

      } 
      break; 
} 
} 

這招在藍牙使用的地址Chat示例應用程序,您可以在SDK的示例文件夾中找到

3

使用此項:

BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address); 
-1

這項工作對我來說:

String macAddress = android.provider.Settings.Secure.getString(mContext.getContentResolver(), "bluetooth_address"); 
相關問題