基本信息
源码名称:android BLE 通信示例源码
源码大小:21.68M
文件格式:.zip
开发语言:Java
更新时间:2018-01-12
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 10 元×
微信扫码支付:10 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
package com.example.bledemo; import java.util.ArrayList; import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.os.Message; import com.bluetooth.Ble; import com.bluetooth.BleBean; import com.bluetooth.BluetoothUtils; public class MainActivity extends Activity { private Ble ble; private BluetoothAdapter mBtAdapter=BluetoothAdapter.getDefaultAdapter(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if(Ble.isSurpportedBle(this)) { ble=new Ble(this,mHandler); new Thread(new Runnable() { @Override public void run() { ble.scanBleDevice(true);//开始扫描ble设备,若要结合蓝牙spp连接方式,可以启动时若在主线程执行该耗时任务可能会使程序卡顿黑屏 } }).start(); //ble.sendCmd(" ",1);发送指令,注意Ble类里修改uuid } } public void BleDiscovery(){ if(!mBtAdapter.isEnabled()) openBlueteeh(); ArrayList<BleBean> devices=ble.getDevices(); /*for (BleBean bluetoothDevice : devices) { LaundryMachine machine = new LaundryMachine(); machine.setDevice(bluetoothDevice.getDevice()); machine.setRssi((short)bluetoothDevice.getRssi()); addDevice(machine); LogInfo("hhh", bluetoothDevice.getDevice() "@" bluetoothDevice.getRssi()); }*/ } private void openBlueteeh() { try { mBtAdapter.enable(); } catch (Exception e) { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, 22); } } //在这里写各个动作 private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case BluetoothUtils.ENABLE_BLUETOOTH: /*Intent intent = new Intent( BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(intent, 1);*/ break; case BluetoothUtils.DEVICE_SCAN_STARTED: break; case BluetoothUtils.DEVICE_SCAN_STOPPED: break; case BluetoothUtils.DEVICE_SCAN_COMPLETED: BleDiscovery(); break; case BluetoothUtils.DEVICE_CONNECTED: break; case BluetoothUtils.DEVICE_CONNECTING: break; case BluetoothUtils.DEVICE_DISCONNECTED: break; case BluetoothUtils.DATA_SENDED: break; case BluetoothUtils.DATA_READED: break; case BluetoothUtils.CHARACTERISTIC_ACCESSIBLE: break; default: break; } } }; @Override public void onStop() { super.onStop(); if(ble!=null) ble.stopGatt(); } }