0
我在我的應用程序中使用藍牙,我想要我的應用程序ti搜索新設備並與它們配對,然後向它們發送預定義的消息,這是我到目前爲止:藍牙不搜索新設備/如何通過藍牙發送字符串消息?
1-這是
public void BlutoothInit()
{
BlutoothListView=(ListView)findViewById(R.id.minimal_profile);
pairedDevices = new ArrayList<String>();
filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
receiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(action)){
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
devices.add(device);
String s = "";
for(int a = 0; a < pairedDevices.size(); a++){
if(device.getName().equals(pairedDevices.get(a))){
s = "(Paired)";
break;
}
}
listAdapter.add(device.getName()+" "+s+" "+"\n"+device.getAddress());
}
else if(BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)){
}
else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
}
else if(BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)){
if(btAdapter.getState() == btAdapter.STATE_OFF){
//turnOnBT();
}
}
}
};
registerReceiver(receiver, filter);
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
registerReceiver(receiver, filter);
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(receiver, filter);
filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(receiver, filter);
}
2 - 她我使用藍牙
public void via_bluetooth (View v) {
getPairedDevices();
final ListView DevicesListView = (ListView) findViewById(R.id.minimal_profile);
ArrayAdapter<String> adapter;
adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,DevicesList);
DevicesListView.setAdapter(adapter);
adapter.notifyDataSetChanged();
DevicesListView.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Toast.makeText(getApplicationContext(), "device is not paired", 0).show();
return true;
}
});
}
的函數初始化我的藍牙功能的變量「G etPairedDevices」工作正常,我得到的一切,我從我的應用程序 之外,但是當我調用這個函數來掃描新設備
public void ScanNewDevices(View v) {
// TODO Auto-generated method stub
if (btAdapter.isDiscovering()) {
btAdapter.cancelDiscovery();
}
btAdapter.startDiscovery();
try{
final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
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 ListView
DevicesList.add(device.getName() + "\n" + device.getAddress());
}
}
};
// Register the BroadcastReceiver
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy
}
catch (Exception e) {
Toast.makeText(getApplicationContext(), e.toString(), 0).show();
}
}
錯誤說做出對設備‘源沒有找到。’出現,什麼都不做! 什麼是iam做錯了什麼是缺少搜索設備和發送消息?
搜索新設備已完成,但如何通過藍牙發送消息? – Mothana
看看這個: http://stackoverflow.com/questions/2071614/source-not-found-android – 2014-06-21 08:27:14