今天给大家带来蓝牙开发的基础,主要展示的是程序搜到蓝牙会通过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 ArrayList<BluetoothDeviceAndRssi> beacons) { /** * 传多个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; List<BluetoothDeviceAndRssi> devices; LayoutInflater inflater; public DeviceAdapter(Context context, List<BluetoothDeviceAndRssi> 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
我们先判断手机能否支持蓝牙功能
<service android:name="com.yunliwuli.beacon.kit.service.UartService" android:enabled="true" />
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
over
需要jar包请私密
作者水平有限,不喜勿喷