我正在尝试扫描该区域中任何广播的BLE设备,并在Android应用中以表格形式显示。但是,当Google Play商店中的其他应用可能时,我的应用无法检测到任何BLE设备。
public class MainActivity extends AppCompatActivity{
//Bluetooth Management
private BluetoothAdapter btAdapter;
private LeDeviceListAdapter mLeDeviceListAdapter;
private boolean mScanning;
// Set the enable bluetooth code
private final static int REQUEST_ENABLE_BT = 0;
// LOG Messages
private final static String DEBUG_TAG = "";
private static final int SCAN_INTERVAL_MS = 10000;
//Handler
private Handler myHandler;
//Testing For ListView
static String[] words = {"one", "two", "three"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myHandler = new Handler();
//Check whether BLE is supported on the device(can selectively disable BLE-related features)
if(!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)){
Toast.makeText(this, "ble_not_supported", Toast.LENGTH_SHORT).show();
finish();
}
final BluetoothManager btManager =
(BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE);
btAdapter = btManager.getAdapter();
//Check if Bluetooth Adapter is present in the device
if(btAdapter == null)
{
Toast.makeText(this, "Error : Bluetooth not supported", Toast.LENGTH_SHORT).show();
Log.i(DEBUG_TAG, "No bluetooth available");
finish();
return;
}
ListView listView = (ListView) findViewById(R.id.ble_Device_List);
listView.setAdapter(new BaseAdapter() {
@Override
public int getCount() {
return 0;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return null;
}
});
}
@Override
public void onResume() {
super.onResume();
// check for Bluetooth enabled on each resume
if (btAdapter != null && !btAdapter.isEnabled()) {
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
}
//Initialise list view adapter
mLeDeviceListAdapter = new LeDeviceListAdapter();
ListView listView = (ListView) findViewById(R.id.ble_Device_List);
listView.setAdapter(mLeDeviceListAdapter);
scanLeDevice(true);
}
@Override
public void onPause() {
super.onPause();
scanLeDevice(false);
mLeDeviceListAdapter.clear();
}
@Override
public void onStop() {
super.onStop();
}
@Override
public void onRestart() {
super.onRestart();
}
@Override
public void onDestroy() {
super.onDestroy();
btAdapter = null;
}
@Override
protected void onActivityResult(int request_enable_bt, int result_enable_bt, Intent data) {
if (result_enable_bt == RESULT_OK) {
//Display "Turn On" message
Toast.makeText(this, "Turned On", Toast.LENGTH_SHORT).show();
Log.i("DEBUG_TOAST", "Turn On Toast Success!");
// Display device name and MAC Address
BluetoothManager btManager =
(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
btAdapter = btManager.getAdapter();
String address = btAdapter.getAddress();
String name = btAdapter.getName();
String toastText = name + " : " + address;
Toast.makeText(this, toastText, Toast.LENGTH_LONG).show();
Log.i("DEBUG_TOAST", "Device Name and Address Toast Success!");
} else if (result_enable_bt == RESULT_CANCELED) {
Toast.makeText(this, "Didn't Turn On", Toast.LENGTH_SHORT).show();
Log.i("DEBUG_TOAST", "Turn Off Toast Success!");
finish();
}
super.onActivityResult(request_enable_bt, result_enable_bt, data);
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.main_menu, menu);
if(!mScanning){
menu.findItem(R.id.scan).setVisible(true);
menu.findItem(R.id.stop).setVisible(false);
menu.findItem(R.id.exit).setVisible(true);
menu.findItem(R.id.info).setVisible(true);
menu.findItem(R.id.refresh).setActionView(null);
Log.i("DEBUG_TOAST", "Menu_mScanning = 0");
}
else{
menu.findItem(R.id.scan).setVisible(false);
menu.findItem(R.id.stop).setVisible(true);
menu.findItem(R.id.exit).setVisible(true);
menu.findItem(R.id.info).setVisible(true);
menu.findItem(R.id.refresh).setActionView(
R.layout.actionbar_indeterminate_progress);
Log.i("DEBUG_TOAST", "Menu_mScanning = 1");
}
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.scan:
if(mLeDeviceListAdapter.getCount() >0)
mLeDeviceListAdapter.clear();
scanLeDevice(true);
Log.i("DEBUG_TOAST", "Scan button pressed!");
break;
case R.id.stop:
scanLeDevice(false);
break;
case R.id.info:
Intent reminder_Intent = new Intent(MainActivity.this, Reminder.class);
startActivity(reminder_Intent);
break;
case R.id.exit:
finish();
break;
}
return true;
}
private BluetoothAdapter.LeScanCallback leScanCallback = 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();
}
});
}
};
private void scanLeDevice(final boolean enable) {
if (enable) {
Log.i("DEBUG_TOAST", "Scanning enabled!");
// Stops scanning after a pre-defined scan period.
myHandler.postDelayed(new Runnable() {
@Override
public void run() {
mScanning = false;
Log.i("DEBUG_TOAST", "scanLeEnable_mScanning = 0");
btAdapter.stopLeScan(leScanCallback);
invalidateOptionsMenu();
}
}, SCAN_INTERVAL_MS);
mScanning = true;
Log.i("DEBUG_TOAST", "scanLeEnable_mScanning = 1");
btAdapter.startLeScan(leScanCallback);
} else {
mScanning = false;
Log.i("DEBUG_TOAST", "scanLeNotEnable_mScanning = 0");
btAdapter.stopLeScan(leScanCallback);
}
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 = MainActivity.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;
}
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder viewHolder;
if (convertView == null){
mInflator =
(LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
convertView = mInflator.inflate(R.layout.listitem_device, null);
viewHolder = new ViewHolder();
viewHolder.deviceAddress = (TextView) convertView.findViewById(R.id.device_address);
Log.i("LISTVIEW_1", "1" );
viewHolder.deviceName = (TextView) convertView.findViewById(R.id.device_name);
convertView.setTag(viewHolder);
}
else{
viewHolder = (ViewHolder) convertView.getTag();
}
BluetoothDevice device = mLeDevices.get(position);
final String deviceName = device.getName();
if(deviceName != null && deviceName.length()>0){
viewHolder.deviceName.setText(deviceName);
Log.i("LISTVIEW_1", "2" );
}
else
viewHolder.deviceName.setText(R.string.unknown_device);
viewHolder.deviceAddress.setText(device.getAddress());
return convertView;
}
}
static class ViewHolder {
TextView deviceAddress, deviceName;
}
}
答案 0 :(得分:0)
您在列表视图中设置了两个不同的适配器。一个是 onResume ,另一个是 onCreate ,你说scanCallback被调用,这意味着显示该项目存在问题。