Android 传统蓝牙(1)
来源:互联网 发布:sql语句在哪里输入 编辑:程序博客网 时间:2024/05/16 10:06
1, /** 打开蓝牙 */
private void open() { mBluetoothAdapter.enable(); }
2,/** 开始扫描蓝牙设备 */
private void startScan() { mBluetoothAdapter.startDiscovery(); }
3,
@Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // 获取被点击的设备 BluetoothDevice device = (BluetoothDevice) parent.getItemAtPosition(position); // 连接设备 startConnect(device); }
}/** 连接到被点击的设备 */
private void startConnect(final BluetoothDevice device) { new Thread(){ public void run() {// BluetoothSocket tmp = null; Method method; try { method = device.getClass().getMethod("createRfcommSocket", new Class[]{int.class}); socket = (BluetoothSocket) method.invoke(device, 1); outputStream = socket.getOutputStream(); } catch (Exception e) {// setState(CONNECT_FAILED); Log.e("TAG", e.toString()); }// socket = tmp; try { socket.connect();// isConnect = true; } catch (Exception e) {// setState(CONNECT_FAILED); Log.e("TAG", e.toString()); } }
注册广播接受者,接收搜索道德蓝@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化view
ListView listView = (ListView) findViewById(R.id.listView);
bluetoothDevices = new ArrayList();
adapter = new BluetoothDeviceListAdapter(bluetoothDevices);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnBluetoothDeviceItemClickListener());
// 初始化蓝牙
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
// 关注新的蓝牙设备信息
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
mBluetoothReceiver = new BluetoothReceiver();
registerReceiver(mBluetoothReceiver, filter); // Don’t forget to unregister during onDestroy
}
private final class BluetoothReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// 发现新设备
// Get the BluetoothDevice object from the Intent BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); // Add the name and address to an array adapter to show in a ListView bluetoothDevices.add(device); adapter.notifyDataSetChanged(); }else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) { // 开始扫描设备 toast("开始扫描设备"); bluetoothDevices.clear(); adapter.notifyDataSetChanged(); }else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { // 停止扫描设备 toast("停止扫描设备"); } }}
“`
- Android 传统蓝牙(1)
- Android传统蓝牙入门
- android 传统蓝牙开发
- 【android 蓝牙开发——传统蓝牙】
- android 传统蓝牙Bluetooth联通性
- Android——搜索传统蓝牙设备
- android 传统蓝牙开发 (附示例源码)
- Android中传统蓝牙模块开发小结
- Android 蓝牙BLE (蓝牙成长之路)1
- Android Bluetooth蓝牙开发:发现Bluetooth蓝牙设备(1)
- 一、Android蓝牙基础(1)
- Android蓝牙基础(1)
- Android 蓝牙开发1
- android -- 蓝牙 bluetooth (二) 打开蓝牙
- android -- 蓝牙 bluetooth (三)搜索蓝牙
- android -- 蓝牙 bluetooth (二) 打开蓝牙 .
- android -- 蓝牙 bluetooth (二) 打开蓝牙
- android -- 蓝牙 bluetooth (三)搜索蓝牙
- android动画(一)逐帧动画
- .NET-BackgroundWorker实现异步处理和滚动条报告进度Demo
- angularjs 删除作用域时清除$watch
- 【模拟】洛谷 P1075 质因数分解
- 关于对编程语言的一点理解
- Android 传统蓝牙(1)
- 设置C++控制台输出行数
- SAP中使用BDC创建或修改采购信息记录
- c++第五次上机作业
- 如何恢复系统桌面文件
- 标准对数视力表与小数记录视力表的换算
- 上下滚动的公告图
- Effective C++ 读书笔记(一)
- web项目中的跨域问题解决方法