लक्ष्य: एक एंड्रॉइड ऐप बनाएं जो बीटी उपकरणों के नाम और पते को सीमा के भीतर खोजता है और अपने मूल्यों को webservice पर सबमिट करता है। बीटी उपकरणों को पहले मेजबान डिवाइस से बंधे नहीं रखा गया है, मैं बस सबकुछ चुनाव करना चाहता हूं क्योंकि मैं चलता हूं।एंड्रॉइड में ब्लूटूथ डिवाइस की खोज - startDiscovery()
मैंने क्या किया है:
प्रलेखन अधिक- Pored।
- होस्ट डिवाइस के बीटी एडाप्टर के स्थानीय उदाहरण को कार्यान्वित किया गया।
- यदि यह सक्षम नहीं है तो बीटी को सक्षम करने के लिए एक अधिसूचना लागू की गई।
- पंजीकृत प्रसारण रिसीवर और
ACTION_FOUNDs
प्रारंभ डिस्कवरी() से बाहर आने के लिए पंजीकृत ब्रॉडकास्ट रिसीवर और इरादे। - पंजीकृत BLUETOOTH और BLUETOOTH_ADMIN मैनिफेस्ट में अनुमतियां।
startDiscovery()
तक चीजें काम (वृद्धिशील कंसोल लॉगिंग के साथ परीक्षण के रूप में)।
कुंठा:
- startDiscovery() - मुझे लगता है मैं गलत संदर्भ में इस गुजर रहा हूँ। सही ढंग से कार्य करने के लिए इस विधि को किस संदर्भ में रखा जाना चाहिए?
यदि आप इस विधि को काम करने में सक्षम हैं, तो मैं आपके ज्ञान की बहुत सराहना करता हूं।
अद्यतन - यहां कोड का एक सरल सरलीकृत संस्करण है जो मुझे दुःख पैदा कर रहा है; यह सरलीकरण मेरी त्रुटि को दोहराता है। यह कोड चलता है, यह 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);
}
}
}
}
यह आपको जो त्रुटि मिल रही है उसे जानने में मदद करेगा ... या कम से कम आपको यह विश्वास करने के लिए प्रेरित कर रहा है कि 'startDiscovery() 'गलत तरीके से कार्य कर रहा है। –