今天給你們帶來藍牙開發的基礎,主要展現的是程序搜到藍牙會經過list展現出來,並實時排序,遠近與信號強弱android
首先咱們要有次jar包ide
咱們先推斷手機是否能支持藍牙功能post
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 適配器全初始化一下this
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);}
藍牙監聽
spa
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地址不變 距離變 鏈接狀態變 */ } };
如下是適配器代碼xml
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包請私密get
做者水平有限。不喜勿噴