2012-05-29 12 views
19

मैं अपने बंधुआ ब्लूटूथ डिवाइस प्राप्त करने की कोशिश कर रहा हूं लेकिन मैं इसे सूची के बजाय एक लंबी स्ट्रिंग के रूप में प्राप्त कर सकता हूं। कुछ इस तरहब्लूटूथ डिवाइस को सूची के रूप में कैसे प्राप्त करें?

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); 
ArrayList<String> listview = 
new ArrayList<String>(Arrays.asList(pairedDevices.toString())); 
setListAdapter(new ArrayAdapter<String>(this, R.layout.list, listview)); 

मैं हो रही है: [00:23:7F:1c, f0:09:f1:b4:b0]

यह मेरा कोड है। और यह सब एक पंक्ति में। मैं इसे एक सूची में बदलने के लिए कैसे बदल सकता हूं और सभी एक पंक्ति में नहीं?

इसके अलावा, मैं डिवाइस के दोस्ताना नाम कैसे प्राप्त कर सकता हूं और इन नंबरों को नहीं?

धन्यवाद !!!

+0

क्या आपने लागू किया था जब सूची के आइटम पर क्लिक करते समय स्वचालित रूप से जोड़े गए डिवाइस से कनेक्ट हो जाता है? –

उत्तर

27

आप नीचे के रूप में अपने कोड बदलना चाहिए:

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); 

List<String> s = new ArrayList<String>(); 
for(BluetoothDevice bt : pairedDevices) 
    s.add(bt.getName()); 

setListAdapter(new ArrayAdapter<String>(this, R.layout.list, s)); 
+0

हाँ !!! धन्यवाद!!!केवल यह जोड़ने की जरूरत है कि मैंने सूची एस = नई ArrayList () बदल दी है; ArrayList एस = नया ArrayList (); यह अब महान काम करता है। – roiberg

+0

अपडेट किए गए उत्तर को देखें – waqaslam

+0

आपके अपडेट के बाद एक ही त्रुटि है। ने वही परिवर्तन किया जो मैंने पहले किया था और यह अब काम करता है। यह त्रुटि थी जिसने मुझे मेरे फिक्स के लिए दिया: प्रकार सूची सामान्य नहीं है; इसे तर्कों के साथ पैरामीटरकृत नहीं किया जा सकता roiberg

9

मैं नीचे दिए गए कोड की कोशिश की,

main.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello" 
    /> 
<TextView 
    android:id="@+id/bluetoothstate" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    /> 
<Button 
    android:id="@+id/listpaireddevices" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="List Paired Devices" 
    android:enabled="false" 
    /> 
<TextView 
    android:id="@+id/bluetoothstate" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    /> 

ListPairedDevicesActivity.java

import java.util.Set; 

import android.app.ListActivity; 
import android.bluetooth.BluetoothAdapter; 
import android.bluetooth.BluetoothClass; 
import android.bluetooth.BluetoothDevice; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 

public class ListPairedDevicesActivity extends ListActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 

    ArrayAdapter<String> btArrayAdapter 
    = new ArrayAdapter<String>(this, 
      android.R.layout.simple_list_item_1); 

    BluetoothAdapter bluetoothAdapter 
    = BluetoothAdapter.getDefaultAdapter(); 
    Set<BluetoothDevice> pairedDevices 
    = bluetoothAdapter.getBondedDevices(); 

    if (pairedDevices.size() > 0) { 
     for (BluetoothDevice device : pairedDevices) { 
     String deviceBTName = device.getName(); 
     String deviceBTMajorClass 
     = getBTMajorDeviceClass(device 
      .getBluetoothClass() 
      .getMajorDeviceClass()); 
     btArrayAdapter.add(deviceBTName + "\n" 
     + deviceBTMajorClass); 
     } 
    } 
    setListAdapter(btArrayAdapter); 

} 

private String getBTMajorDeviceClass(int major){ 
    switch(major){ 
    case BluetoothClass.Device.Major.AUDIO_VIDEO: 
    return "AUDIO_VIDEO"; 
    case BluetoothClass.Device.Major.COMPUTER: 
    return "COMPUTER"; 
    case BluetoothClass.Device.Major.HEALTH: 
    return "HEALTH"; 
    case BluetoothClass.Device.Major.IMAGING: 
    return "IMAGING"; 
    case BluetoothClass.Device.Major.MISC: 
    return "MISC"; 
    case BluetoothClass.Device.Major.NETWORKING: 
    return "NETWORKING"; 
    case BluetoothClass.Device.Major.PERIPHERAL: 
    return "PERIPHERAL"; 
    case BluetoothClass.Device.Major.PHONE: 
    return "PHONE"; 
    case BluetoothClass.Device.Major.TOY: 
    return "TOY"; 
    case BluetoothClass.Device.Major.UNCATEGORIZED: 
    return "UNCATEGORIZED"; 
    case BluetoothClass.Device.Major.WEARABLE: 
    return "AUDIO_VIDEO"; 
    default: return "unknown!"; 
    } 
} 

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 
    // TODO Auto-generated method stub 
    super.onListItemClick(l, v, position, id); 

    Intent intent = new Intent(); 
    setResult(RESULT_OK, intent); 
    finish(); 
} 

} 

AndroidBluetooth.java

import android.app.Activity; 
import android.bluetooth.BluetoothAdapter; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

public class AndroidBluetooth extends Activity { 

private static final int REQUEST_ENABLE_BT = 1; 
private static final int REQUEST_PAIRED_DEVICE = 2; 

    /** Called when the activity is first created. */ 
Button btnListPairedDevices; 
TextView stateBluetooth; 
BluetoothAdapter bluetoothAdapter; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     btnListPairedDevices = (Button)findViewById(R.id.listpaireddevices); 

     stateBluetooth = (TextView)findViewById(R.id.bluetoothstate); 
     bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 

     CheckBlueToothState(); 

     btnListPairedDevices.setOnClickListener(btnListPairedDevicesOnClickListener); 
    } 

    private void CheckBlueToothState(){ 
    if (bluetoothAdapter == null){ 
     stateBluetooth.setText("Bluetooth NOT support"); 
     }else{ 
     if (bluetoothAdapter.isEnabled()){ 
      if(bluetoothAdapter.isDiscovering()){ 
      stateBluetooth.setText("Bluetooth is currently in device discovery process."); 
      }else{ 
      stateBluetooth.setText("Bluetooth is Enabled."); 
      btnListPairedDevices.setEnabled(true); 
      } 
     }else{ 
      stateBluetooth.setText("Bluetooth is NOT Enabled!"); 
      Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
      startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); 
     } 
     } 
    } 

    private Button.OnClickListener btnListPairedDevicesOnClickListener 
    = new Button.OnClickListener(){ 

    @Override 
    public void onClick(View arg0) { 
    // TODO Auto-generated method stub 
    Intent intent = new Intent(); 
    intent.setClass(AndroidBluetooth.this, ListPairedDevicesActivity.class); 
    startActivityForResult(intent, REQUEST_PAIRED_DEVICE); 
    }}; 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    // TODO Auto-generated method stub 
    if(requestCode == REQUEST_ENABLE_BT){ 
    CheckBlueToothState(); 
    }if (requestCode == REQUEST_PAIRED_DEVICE){ 
    if(resultCode == RESULT_OK){ 

    } 
    } 
} 
} 

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
     package="com.test.AndroidBluetooth" 
     android:versionCode="1" 
     android:versionName="1.0"> 
    <uses-sdk android:minSdkVersion="7" /> 
    <uses-permission android:name="android.permission.BLUETOOTH"></uses-permission> 

    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
     <activity android:name=".AndroidBluetooth" 
        android:label="@string/app_name"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    <activity android:name=".ListPairedDevicesActivity" 
     android:label="AndroidBluetooth: List of Paired Devices"/> 
    </application> 
</manifest> 
+0

क्या यह एक क्लिक के साथ युग्मित डिवाइस से कनेक्ट होता है? –

24

आस-पास के ब्लूटूथ उपकरणों के सूची खोजें:

इसके लिए स्क्रीनशॉट खोजें।

enter image description here

MainActivity.java:

public class MainActivity extends ActionBarActivity { 

    private ListView listView; 
    private ArrayList<String> mDeviceList = new ArrayList<String>(); 
    private BluetoothAdapter mBluetoothAdapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     listView = (ListView) findViewById(R.id.listView); 

     mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
     mBluetoothAdapter.startDiscovery(); 

     IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); 
     registerReceiver(mReceiver, filter); 

    } 


    @Override 
    protected void onDestroy() { 
     unregisterReceiver(mReceiver); 
     super.onDestroy(); 
    } 

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 
     public void onReceive(Context context, Intent intent) { 
      String action = intent.getAction(); 
      if (BluetoothDevice.ACTION_FOUND.equals(action)) { 
       BluetoothDevice device = intent 
         .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
       mDeviceList.add(device.getName() + "\n" + device.getAddress()); 
       Log.i("BT", device.getName() + "\n" + device.getAddress()); 
       listView.setAdapter(new ArrayAdapter<String>(context, 
         android.R.layout.simple_list_item_1, mDeviceList)); 
      } 
     } 
    }; 

activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.bluetoothdemo.MainActivity" > 

    <ListView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/listView"/> 

</RelativeLayout> 

प्रकट फ़ाइल:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.bluetoothdemo" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="21" /> 

    <uses-permission android:name="android.permission.BLUETOOTH" /> 
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name=".MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

+0

मैन जो महान है। – DavidBalas

+0

@ डेविड बालास, आपकी मदद करने में खुशी हुई। –

+0

क्या यह एक युग्मित डिवाइस से कनेक्ट होता है? –

1

हो गया इस कोड को आप बस अपने बटन क्लिक में इस कॉल करने की आवश्यकता है।

private void list_paired_Devices() { 
     Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); 
     ArrayList<String> devices = new ArrayList<>(); 
     for (BluetoothDevice bt : pairedDevices) { 
      devices.add(bt.getName() + "\n" + bt.getAddress()); 
     } 
     ArrayAdapter arrayAdapter = new ArrayAdapter(bluetooth.this, android.R.layout.simple_list_item_1, devices); 
     emp.setAdapter(arrayAdapter); 
    } 
संबंधित मुद्दे