今天给大家带来蓝牙开发的基础,主要展示的是程序搜到蓝牙会通过list展示出来,并实时排序,远近与信号强弱
首先我们要有次jar包
我们先推断手机是否能支持蓝牙功能
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) { Toast.makeText(this, "蓝牙不支持", Toast.LENGTH_SHORT).show();}
然后我们看看蓝牙是否打开
if (!ylwlmanager.isBluetoothEnabled()) { Toast.makeText(this, "蓝牙没有打开", Toast.LENGTH_SHORT).show();}
把list 适配器全初始化一下
private void UIinit() {
final YlwlManager ylwlmanager = YlwlManager.getInstance(mcontext);newDevicesListView = (ListView) findViewById(R.id. new_devices) ; deviceAdapter = new DeviceAdapter( this, deviceList) ; newDevicesListView.setAdapter( deviceAdapter) ;
//把蓝牙接收打开 这里能够用个button控制 作者直接就打开了
ylwlmanager.scanLeDevice(true); ylwlmanager.setYlwlManagerListener(lis);}
蓝牙监听
YlwlManagerListener lis = new YlwlManagerListener() { @Override public void onUpdateBeacon(final ArrayListbeacons) { /** * 传多个beacon过来 已经做好了排序 , 距离 连接状态(BluetoothDeviceAndRssi isConn方法) * 也随时改变 */ runOnUiThread(new Runnable() { @Override public void run() { deviceList = beacons; Collections.sort(deviceList);// 距离排序 deviceAdapter = new DeviceAdapter(DeviceListActivity.this, deviceList); newDevicesListView.setAdapter(deviceAdapter); } }); } @Override public void onNewBeacon(BluetoothDeviceAndRssi beacon) { /** * 传单个beacon过来 */ } @Override public void onNewBeaconDataChang(BluetoothDeviceAndRssi beacon) { /** * 传单个beacon过来 并且是mac地址不变 距离变 连接状态变 */ }};
以下是适配器代码
public class DeviceAdapter extends BaseAdapter { Context context; Listdevices; LayoutInflater inflater; public DeviceAdapter(Context context, List devices) { this.context = context; inflater = LayoutInflater.from(context); this.devices = devices; } @Override public int getCount() { return devices.size(); } @Override public Object getItem(int position) { return devices.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewGroup vg = null; if (convertView != null) { vg = (ViewGroup) convertView; } else { vg = (ViewGroup) inflater .inflate(R.layout.device_item, null); } BluetoothDeviceAndRssi bluetoothdeviceandrssi = devices .get(position); TextView tvadd = ((TextView) vg.findViewById(R.id.address)); TextView tvname = ((TextView) vg.findViewById(R.id.name)); ImageView image_rssi = (ImageView) vg.findViewById(R.id.image_rssi); byte rssival = (byte) bluetoothdeviceandrssi.getRssi(); if (rssival > (-60)) { image_rssi.setImageResource(R.drawable.icon_rssi6); } else if (rssival > (-75)) { image_rssi.setImageResource(R.drawable.icon_rssi5); } else if (rssival > (-90)) { image_rssi.setImageResource(R.drawable.icon_rssi4); } else if (rssival > (-100)) { image_rssi.setImageResource(R.drawable.icon_rssi3); } else if (rssival > (-110)) { image_rssi.setImageResource(R.drawable.icon_rssi2); } else { image_rssi.setImageResource(R.drawable.icon_rssi1); } if (bluetoothdeviceandrssi.getBluetoothdevice().getName() == null) { tvname.setText("Unknow Name"); } else { tvname.setText(bluetoothdeviceandrssi.getBluetoothdevice() .getName()); } tvadd.setText("mac地址"+ bluetoothdeviceandrssi.getBluetoothdevice().getAddress()); return vg; } }
图片是这个样子
最后重要的是 AndroidManifest.xml
我们先推断手机是否能支持蓝牙功能
权限
over
须要jar包请私密
作者水平有限。不喜勿喷
我们先推断手机是否能支持蓝牙功能