12

目標:構建一個Android應用程序,用於發現範圍內BT設備的名稱和地址並將其值提交給web服務。 BT設備以前沒有被綁定到主機設備,我只是想在我走過的時候輪詢一切。Android中的藍牙設備發現 - startDiscovery()

我所做的:

  1. 看了又看文檔。
  2. 實施主機設備的BT適配器的本地實例。
  3. 如果未啓用,則實施通知以啓用BT。
  4. 註冊廣播接收器和意圖解析ACTION_FOUNDs來自startDiscovery()
  5. 註冊BLUETOOTHBLUETOOTH_ADMIN清單中的權限。

事情工作(如增量控制檯日誌記錄測試)直到startDiscovery()


無奈:

  • startDiscovery() - 我懷疑我傳遞這在錯誤的上下文。這種方法需要放在什麼上下文才能正常工作?

如果你已經能夠得到這個方法的工作,我會非常感謝你的智慧。

UPDATE - 這是簡化版本的代碼,這讓我很悲傷;這種簡化重演了我的錯誤。此代碼運行,它不會拋出cat.log錯誤或其他錯誤,它根本不會給出任何輸出。

package aqu.bttest; 

import android.app.Activity; 
import android.bluetooth.BluetoothAdapter; 
import android.bluetooth.BluetoothDevice; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.os.Bundle; 
import android.widget.Toast; 

public class BT2Activity extends Activity { 

private BluetoothAdapter mBTA; 
private SingBroadcastReceiver mReceiver; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    //register local BT adapter 
    mBTA = BluetoothAdapter.getDefaultAdapter(); 
    //check to see if there is BT on the Android device at all 
    if (mBTA == null){ 
     int duration = Toast.LENGTH_SHORT; 
     Toast.makeText(this, "No Bluetooth on this handset", duration).show(); 
    } 
    //let's make the user enable BT if it isn't already 
    if (!mBTA.isEnabled()){ 
     Intent enableBT = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
     startActivityForResult(enableBT, 0xDEADBEEF); 
    } 
    //cancel any prior BT device discovery 
    if (mBTA.isDiscovering()){ 
     mBTA.cancelDiscovery(); 
    } 
    //re-start discovery 
    mBTA.startDiscovery(); 

    //let's make a broadcast receiver to register our things 
    mReceiver = new SingBroadcastReceiver(); 
    IntentFilter ifilter = new IntentFilter(BluetoothDevice.ACTION_FOUND); 
    this.registerReceiver(mReceiver, ifilter); 
} 

private class SingBroadcastReceiver extends BroadcastReceiver { 

    public void onReceive(Context context, Intent intent) { 
     String action = intent.getAction(); //may need to chain this to a recognizing function 
     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 Toast 
      String derp = device.getName() + " - " + device.getAddress(); 
      Toast.makeText(context, derp, Toast.LENGTH_LONG); 
     } 
    } 
} 

}

+0

這將有助於瞭解您所得到的錯誤......或者至少是什麼讓您相信'startDiscovery()'運行不正常。 –

回答

15

確實需要何種情況下這種方法被妥善安置到函數中。

簡單地說,你應該使用startDiscovery()當你想你的應用程序發現本地的藍牙設備......舉例來說,如果你想實現一個ListActivity,掃描和動態地將附近的藍牙設備的ListView(見DeviceListActivity)。

你的startDiscovery()方法的使用應該是這個樣子:

  1. 定義表示本地藍牙適配器類變量。

    BluetoothAdapter mBtAdapter = BluetoothAdapter.getDefaultAdapter(); 
    
  2. 檢查您的設備是否已經「發現」。如果是,則取消發現。

    if (mBtAdapter.isDiscovering()) { 
        mBtAdapter.cancelDiscovery(); 
    } 
    
  3. 檢查(也可能取消)發現模式後,立即致電開始發現,

    mBtAdapter.startDiscovery(); 
    
  4. 是一般非常小心,不小心讓你的設備處於發現模式。執行設備發現對於藍牙適配器來說是一個沉重的過程,並且會消耗大量資源。例如,您想確保在嘗試建立連接之前檢查/取消發現。您最有可能還想在您的onDestroy方法中取消發現。

讓我知道,如果這有助於...如果你仍然有問題,你的logcat的輸出和/或您收到任何錯誤消息更新你的答案,也許我可以幫你出位更多。

+0

這有幫助。此外,將結果傳遞給ArrayAdapter而不是Toast也有幫助。 – Lemminkainen

+0

有沒有什麼辦法可以在BG服務中保持連續掃描,而不需要放置Timer ...任何系統級別的API。 – CoDe

+0

@Shubh我不這麼認爲。這種限制的原因在於確保應用程序不會通過在後臺連續掃描來濫用「藍牙」。 –