2014-04-16 11 views
5

क्या आप मुझे आस-पास के बीएलई उपकरणों को स्कैन करने और डिवाइस नाम और मैक आईडी द्वारा सूचीबद्ध करने के लिए सरल कोड प्रदान कर सकते हैं। मैंने http://developer.android.com/guide/topics/connectivity/bluetooth-le.html में दिए गए नमूना कोड का उपयोग करके इसे आजमाया। लेकिन काम नहीं किया, कोई संदर्भ लिंक या विचार क्योंकि मैं बीएलई ऐप के लिए नया हूं।डिवाइस स्कैन के बाद एंड्रॉइड लिस्टिंग बीएलई डिवाइस

उत्तर

20

यह उदाहरण आपके द्वारा पोस्ट किए गए डेवलपर वेब पर आधारित है और मेरे लिए बहुत अच्छा काम करता है।

DeviceScanActivity.class

package com.example.android.bluetoothlegatt; 

import android.app.Activity; 
import android.app.ListActivity; 
import android.bluetooth.BluetoothAdapter; 
import android.bluetooth.BluetoothDevice; 
import android.bluetooth.BluetoothManager; 
import android.content.Context; 
import android.content.Intent; 
import android.content.pm.PackageManager; 
import android.os.Bundle; 
import android.os.Handler; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.ListView; 
import android.widget.TextView; 
import android.widget.Toast; 
import java.util.ArrayList; 

public class DeviceScanActivity extends ListActivity { 
private LeDeviceListAdapter mLeDeviceListAdapter; 
private BluetoothAdapter mBluetoothAdapter; 
private boolean mScanning; 
private Handler mHandler; 

private static final int REQUEST_ENABLE_BT = 1; 
// Stops scanning after 10 seconds. 
private static final long SCAN_PERIOD = 10000; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    getActionBar().setTitle(R.string.title_devices); 
    mHandler = new Handler(); 

    // Use this check to determine whether BLE is supported on the device. Then you can 
    // selectively disable BLE-related features. 
    if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) { 
     Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show(); 
     finish(); 
    } 
    // Initializes a Bluetooth adapter. For API level 18 and above, get a reference to 
    // BluetoothAdapter through BluetoothManager. 
    final BluetoothManager bluetoothManager = 
      (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); 
    mBluetoothAdapter = bluetoothManager.getAdapter(); 
    // Checks if Bluetooth is supported on the device. 
    if (mBluetoothAdapter == null) { 
     Toast.makeText(this, R.string.error_bluetooth_not_supported, Toast.LENGTH_SHORT).show(); 
     finish(); 
     return; 
    } 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.main, menu); 
    if (!mScanning) { 
     menu.findItem(R.id.menu_stop).setVisible(false); 
     menu.findItem(R.id.menu_scan).setVisible(true); 
     menu.findItem(R.id.menu_refresh).setActionView(null); 
    } else { 
     menu.findItem(R.id.menu_stop).setVisible(true); 
     menu.findItem(R.id.menu_scan).setVisible(false); 
     menu.findItem(R.id.menu_refresh).setActionView(
       R.layout.actionbar_indeterminate_progress); 
    } 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case R.id.menu_scan: 
      mLeDeviceListAdapter.clear(); 
      scanLeDevice(true); 
      break; 
     case R.id.menu_stop: 
      scanLeDevice(false); 
      break; 
    } 
    return true; 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
    // Ensures Bluetooth is enabled on the device. If Bluetooth is not currently enabled, 
    // fire an intent to display a dialog asking the user to grant permission to enable it. 
    if (!mBluetoothAdapter.isEnabled()) { 
     if (!mBluetoothAdapter.isEnabled()) { 
      Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
      startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); 
     } 
    } 
    // Initializes list view adapter. 
    mLeDeviceListAdapter = new LeDeviceListAdapter(); 
    setListAdapter(mLeDeviceListAdapter); 
    scanLeDevice(true); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    // User chose not to enable Bluetooth. 
    if (requestCode == REQUEST_ENABLE_BT && resultCode == Activity.RESULT_CANCELED) { 
     finish(); 
     return; 
    } 
    super.onActivityResult(requestCode, resultCode, data); 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    scanLeDevice(false); 
    mLeDeviceListAdapter.clear(); 
} 

private void scanLeDevice(final boolean enable) { 
    if (enable) { 
     // Stops scanning after a pre-defined scan period. 
     mHandler.postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       mScanning = false; 
       mBluetoothAdapter.stopLeScan(mLeScanCallback); 
       invalidateOptionsMenu(); 
      } 
     }, SCAN_PERIOD); 
     mScanning = true; 
     mBluetoothAdapter.startLeScan(mLeScanCallback); 
    } else { 
     mScanning = false; 
     mBluetoothAdapter.stopLeScan(mLeScanCallback); 
    } 
    invalidateOptionsMenu(); 
} 

// Adapter for holding devices found through scanning. 
private class LeDeviceListAdapter extends BaseAdapter { 
    private ArrayList<BluetoothDevice> mLeDevices; 
    private LayoutInflater mInflator; 

    public LeDeviceListAdapter() { 
     super(); 
     mLeDevices = new ArrayList<BluetoothDevice>(); 
     mInflator = DeviceScanActivity.this.getLayoutInflater(); 
    } 

    public void addDevice(BluetoothDevice device) { 
     if(!mLeDevices.contains(device)) { 
      mLeDevices.add(device); 
     } 
    } 

    public BluetoothDevice getDevice(int position) { 
     return mLeDevices.get(position); 
    } 

    public void clear() { 
     mLeDevices.clear(); 
    } 

    @Override 
    public int getCount() { 
     return mLeDevices.size(); 
    } 

    @Override 
    public Object getItem(int i) { 
     return mLeDevices.get(i); 
    } 

    @Override 
    public long getItemId(int i) { 
     return i; 
    } 

    @Override 
    public View getView(int i, View view, ViewGroup viewGroup) { 
     ViewHolder viewHolder; 
     // General ListView optimization code. 
     if (view == null) { 
      view = mInflator.inflate(R.layout.listitem_device, null); 
      viewHolder = new ViewHolder(); 
      viewHolder.deviceAddress = (TextView) view.findViewById(R.id.device_address); 
      viewHolder.deviceName = (TextView) view.findViewById(R.id.device_name); 
      view.setTag(viewHolder); 
     } else { 
      viewHolder = (ViewHolder) view.getTag(); 
     } 

     BluetoothDevice device = mLeDevices.get(i); 
     final String deviceName = device.getName(); 
     if (deviceName != null && deviceName.length() > 0) 
      viewHolder.deviceName.setText(deviceName); 
     else 
      viewHolder.deviceName.setText(R.string.unknown_device); 
     viewHolder.deviceAddress.setText(device.getAddress()); 

     return view; 
    } 
} 

// Device scan callback. 
private BluetoothAdapter.LeScanCallback mLeScanCallback = 
     new BluetoothAdapter.LeScanCallback() { 

    @Override 
    public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) { 
     runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 
       mLeDeviceListAdapter.addDevice(device); 
       mLeDeviceListAdapter.notifyDataSetChanged(); 
      } 
     }); 
    } 
}; 

static class ViewHolder { 
    TextView deviceName; 
    TextView deviceAddress; 
} 

}

सूची दृश्य के लिए कस्टम लेआउट listitem_device.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 
    <TextView android:id="@+id/device_name" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:textSize="24dp"/> 
    <TextView android:id="@+id/device_address" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:textSize="12dp"/> 
</LinearLayout> 

स्कैनिंग actionbar_indeterminate_progress.xml पर प्रगति बार इस कोड है:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_height="wrap_content" 
     android:layout_width="56dp" 
     android:minWidth="56dp"> 
<ProgressBar android:layout_width="32dp" 
      android:layout_height="32dp" 
      android:layout_gravity="center"/> 
</FrameLayout> 

मेनू लेआउट main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:id="@+id/menu_refresh" 
     android:checkable="false" 
     android:orderInCategory="1" 
     android:showAsAction="ifRoom"/> 
<item android:id="@+id/menu_scan" 
     android:title="@string/menu_scan" 
     android:orderInCategory="100" 
     android:showAsAction="ifRoom|withText"/> 
<item android:id="@+id/menu_stop" 
     android:title="@string/menu_stop" 
     android:orderInCategory="101" 
     android:showAsAction="ifRoom|withText"/> 
</menu> 

तार लेआउट strings.xml:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <string name="ble_not_supported">BLE is not supported</string> 
    <string name="error_bluetooth_not_supported">Bluetooth not supported.</string> 
    <string name="unknown_device">Unknown device</string> 

    <!-- Menu items --> 
    <string name="menu_connect">Connect</string> 
    <string name="menu_disconnect">Disconnect</string> 
    <string name="menu_scan">Scan</string> 
    <string name="menu_stop">Stop</string> 
</resources> 

और प्रकट AndroidManifest.xml:

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

<uses-sdk android:minSdkVersion="18" 
    android:targetSdkVersion="19"/> 
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/> 

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

<application android:label="@string/app_name" 
    android:icon="@drawable/ic_launcher" 
    android:theme="@android:style/Theme.Holo.Light"> 
    <activity android:name=".DeviceScanActivity" 
     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> 

मुझे लगता है कि यह सब कुछ है। अगर मुझे कुछ याद आया तो मुझे अब चलो और मैं इसे ठीक कर दूं। आशा करता हूँ की ये काम करेगा!!

(ब्लूटूथ LE काफी एंड्रॉयड में बेकार है अभी तक: डी ... जरूरतों और तेजी से अद्यतन)

अद्यतन:

डाउनलोड यहाँ BLE स्कैन और कनेक्शन की एक पूरी उदाहरण: https://dl.dropboxusercontent.com/u/18548987/DeviceScanActivity.rar

+1

आपके मूल्यवान समय के लिए धन्यवाद।मैं जल्द ही आपको जांचूंगा और आपको बता दूंगा :) – info

+0

हाय, मुझे क्रैश त्रुटि मिल रही है डी/AbsListView (17 995): unregisterIRListener() को कॉल किया जाता है। सैमसंग गैलेक्सी एस 4 में चलते समय। Plz मदद – info

+1

मुझे वास्तव में पता नहीं है कि वह त्रुटि क्या है। वैसे भी, पूर्ण प्रोजेक्ट डाउनलोड करें और आज़माएं जो आपको ब्ली डिवाइस से कनेक्ट करने और उनकी सेवाओं की विशेषताओं को देखने की अनुमति देगा: https://www.dropbox.com/s/66y8j9auptllbhf/DeviceScanActivity.rar – margabro

4

यह काफी पुराना सवाल है, लेकिन भविष्य के पाठकों के लिए मैं सिर्फ ब्लूटूथ एसआईजी द्वारा प्रदान किए गए आधिकारिक स्रोत कोडों का निरीक्षण करना चाहता हूं:

Application Accelerator

सबसे मोबाइल प्लेटफ़ॉर्म (Android, iOS, विंडोज फोन और अधिक) + कुछ सामग्री/ट्यूटोरियल के लिए, छोटे समझने में आसान और दस्तावेज क्षुधा हैं। यदि आप बीएलई के साथ खेलना शुरू करना चाहते हैं तो यह मेरी राय में सबसे अच्छा प्रारंभिक बिंदु है।

सब कुछ मुफ्त है, लेकिन आपको वेबसाइट पर पंजीकरण करने की आवश्यकता है। जहां तक ​​मुझे याद है कि सालाना 1-3 ईमेल हो सकता है, सभी ब्लूटूथ विकास के लिए नए उपकरणों से जुड़े हुए हैं।

डेरेक

+1

के अंदर 'ऑन-कैरेक्टरिस्ट रीड' में मान प्राप्त होगा, एंड्रॉइड 6.0 समर्थन के साथ इसका एक नया संस्करण है: https://www.bluetooth.com/develop-with-bluetooth/developer-resources-tools/ऐप-एसीसी -2 –

+0

धन्यवाद, बीएलई स्कैनर काम नहीं करने के कई उदाहरणों में से, यह एक (Evin1_ का अंतिम लिंक) ठीक काम करता है। – Ujeenator

संबंधित मुद्दे