本文介绍了清单Android的蓝牙设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是完全的清醒,我得到了我的应用程序的工作100%(这只是上市没有做任何连接设备),但是当我执行它它不出现任何蓝牙设备在我的ListView,最有趣的部分是,当我执行它与调试,但会出现。在这里,你有我的code,我一直在寻找它在过去的3个小时,希望你可以登陆我的手:(
进口android.os.Bundle;
进口android.app.Activity;
进口android.app.ListActivity;
进口android.bluetooth.BluetoothAdapter;
进口android.bluetooth.BluetoothDevice;
进口android.content.BroadcastReceiver;
进口android.content.Context;
进口android.content.Intent;
进口android.content.IntentFilter;
进口android.view.Menu;
进口android.view.View;
进口android.widget.AdapterView;
进口android.widget.AdapterView.OnItemClickListener;
进口android.widget.ArrayAdapter;
进口android.widget.ListView;
进口android.widget.TextView;
进口android.widget.Toast;公共类ListarDispositivos扩展ListActivity {
//这个,R.layout.activity_listar_dispositivos,S 私人BluetoothAdapter mBtAdapter;
私人ArrayAdapter<串GT; mNewDevicesArrayAdapter;
私人静态字符串EXTRA_DEVICE_ADDRESS =DEVICE_ADDRESS; @覆盖
保护无效的onCreate(捆绑savedInstanceState){ super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_listar_dispositivos); mNewDevicesArrayAdapter =新ArrayAdapter<串GT;(这一点,R.layout.nombres_dispositivos,R.id.txvNombreDispositivo); ListView控件列表视图= getListView(); setListAdapter(mNewDevicesArrayAdapter);
//listview.setOnItemClickListener(mDeviceClickListener); IntentFilter的过滤器=新的IntentFilter(BluetoothDevice.ACTION_FOUND);
this.registerReceiver(mReceiver,过滤器); 过滤器=新的IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
this.registerReceiver(mReceiver,过滤器); mBtAdapter = BluetoothAdapter.getDefaultAdapter(); doDiscovery(); 意向discoverableIntent =新
意图(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,300);
startActivity(discoverableIntent);
} 保护无效的onDestroy(){
super.onDestroy(); //确保我们没有做再发现
如果(mBtAdapter!= NULL){
mBtAdapter.cancelDiscovery();
} mBtAdapter.disable(); //注销广播监听器
this.unregisterReceiver(mReceiver); } 私人无效doDiscovery(){ //表明,在标题扫描
的setTitle(Escaneando); //如果我们已经发现,阻止它
如果(mBtAdapter.isDiscovering()){
mBtAdapter.cancelDiscovery();
} //请求从BluetoothAdapter发现
mBtAdapter.startDiscovery();
吐司toast1 =
Toast.makeText(getApplicationContext(),
埃斯托伊buscando,Toast.LENGTH_SHORT); toast1.show(); } 私人OnItemClickListener mDeviceClickListener =新OnItemClickListener(){
公共无效onItemClick(适配器视图<> AV,视图V,INT ARG2,长ARG3){
//取消的发现,因为它是昂贵的,我们将要连接
mBtAdapter.cancelDiscovery(); //获取设备的MAC地址,这是在视图中最后一个字符17
字符串信息=((的TextView)V).getText()的toString()。
字符串的地址= info.substring(info.length() - 17); //创建结果意向,包括MAC地址
意向意图=新的Intent();
intent.putExtra(EXTRA_DEVICE_ADDRESS,地址); //设置结果,并完成本活动
的setResult(Activity.RESULT_OK,意向);
完();
}
}; //萨尔塔CADA VEZ阙encuentra联合国dispositivoØESO deberia埃尔cabron 私人最终的BroadcastReceiver mReceiver =新的广播接收器(){
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
字符串行动= intent.getAction();
//当发现找到的设备
如果(BluetoothDevice.ACTION_FOUND.equals(动作)){
//从意向获取BluetoothDevice类对象
BluetoothDevice类设备= intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
//如果它已经配对,跳过它,因为它已经被列入
如果(device.getBondState()!= BluetoothDevice.BOND_BONDED){
mNewDevicesArrayAdapter.add(device.getName()+\\ n+ device.getAddress());
}
//当发现完成后,更改活动标题
}否则如果(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(动作)){ 的setTitle(Elija dispositivo);
如果(mNewDevicesArrayAdapter.getCount()== 0){
字符串nodevices选项=没有encontrado
mNewDevicesArrayAdapter.add(nodevices选项);
}
}
} };
@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
//充气菜单;如果是present这增加了项目操作栏。
。getMenuInflater()膨胀(R.menu.listar_dispositivos,菜单);
返回true;
}}
解决方案
将发现的设备在 mNewDevicesArrayAdapter
,你应该叫 notifyDataSetChanged()
让列表视图刷新。
This is totally in sane, I got my app working 100% (it's only listing devices not doing any connection) but when i execute it it doesnt appear any Bluetooth device in my ListView, the funniest part is that when I execute it with debugger they DO appear. Here you have my code, I have been looking at it for the last 3 hours, hope you can land me a hand :(
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
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.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class ListarDispositivos extends ListActivity {
//this, R.layout.activity_listar_dispositivos, s
private BluetoothAdapter mBtAdapter;
private ArrayAdapter<String> mNewDevicesArrayAdapter;
private static String EXTRA_DEVICE_ADDRESS = "device_address";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listar_dispositivos);
mNewDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.nombres_dispositivos,R.id.txvNombreDispositivo);
ListView listview = getListView();
setListAdapter(mNewDevicesArrayAdapter);
//listview.setOnItemClickListener(mDeviceClickListener);
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
this.registerReceiver(mReceiver, filter);
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
this.registerReceiver(mReceiver, filter);
mBtAdapter = BluetoothAdapter.getDefaultAdapter();
doDiscovery();
Intent discoverableIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity(discoverableIntent);
}
protected void onDestroy() {
super.onDestroy();
// Make sure we're not doing discovery anymore
if (mBtAdapter != null) {
mBtAdapter.cancelDiscovery();
}
mBtAdapter.disable();
// Unregister broadcast listeners
this.unregisterReceiver(mReceiver);
}
private void doDiscovery() {
// Indicate scanning in the title
setTitle("Escaneando");
// If we're already discovering, stop it
if (mBtAdapter.isDiscovering()) {
mBtAdapter.cancelDiscovery();
}
// Request discover from BluetoothAdapter
mBtAdapter.startDiscovery();
Toast toast1 =
Toast.makeText(getApplicationContext(),
"Estoy buscando", Toast.LENGTH_SHORT);
toast1.show();
}
private OnItemClickListener mDeviceClickListener = new OnItemClickListener() {
public void onItemClick(AdapterView<?> av, View v, int arg2, long arg3) {
// Cancel discovery because it's costly and we're about to connect
mBtAdapter.cancelDiscovery();
// Get the device MAC address, which is the last 17 chars in the View
String info = ((TextView)v).getText().toString();
String address = info.substring(info.length() - 17);
// Create the result Intent and include the MAC address
Intent intent = new Intent();
intent.putExtra(EXTRA_DEVICE_ADDRESS, address);
// Set result and finish this Activity
setResult(Activity.RESULT_OK, intent);
finish();
}
};
//Salta cada vez que encuentra un dispositivo o eso deberia el cabron
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
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);
// If it's already paired, skip it, because it's been listed already
if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}
// When discovery is finished, change the Activity title
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
setTitle("Elija dispositivo");
if (mNewDevicesArrayAdapter.getCount() == 0) {
String noDevices = "No encontrado";
mNewDevicesArrayAdapter.add(noDevices);
}
}
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.listar_dispositivos, menu);
return true;
}
}
解决方案
After adding found devices to the mNewDevicesArrayAdapter
, you should call notifyDataSetChanged()
so that the listview is refreshed.
这篇关于清单Android的蓝牙设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!