android_蓝牙模块的开发
来源:互联网 发布:15个java小项目 文库 编辑:程序博客网 时间:2024/06/04 22:20
android代码实现手机和蓝牙板块之间的通讯
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="scan" android:text="扫描蓝牙设备" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="stop" android:text="停止蓝牙扫描" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="switchLight" android:text="开关电灯" /> <LinearLayout android:id="@+id/ll_bluetooth" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" > </LinearLayout></LinearLayout>
public class MainActivity extends Activity { BluetoothAdapter mBluetoothAdapter; BluetoothReceiver mBluetoothReceiver; LinearLayout mLl_bluetooth; OutputStream os; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); mLl_bluetooth = (LinearLayout) findViewById(R.id.ll_bluetooth); mBluetoothAdapter.enable(); IntentFilter filter = new IntentFilter(); // 开始扫描的广播 filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED); // 扫描完成的广播 filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); // 发现一个可用的设备的广播 filter.addAction(BluetoothDevice.ACTION_FOUND); mBluetoothReceiver = new BluetoothReceiver(); // 注册监听 registerReceiver(mBluetoothReceiver, filter); } private class BluetoothReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) { Toast.makeText(MainActivity.this, "开始扫描蓝牙设备", 0).show(); } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED .equals(action)) { Toast.makeText(MainActivity.this, "扫描蓝牙设备完成", 0).show(); } else if (BluetoothDevice.ACTION_FOUND.equals(action)) { Toast.makeText(MainActivity.this, "发现了蓝牙设备", 0).show(); final BluetoothDevice device = intent .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); // 打印出附近的蓝牙设备 System.out.println(device.getName() + "\n" + device.getAddress()); TextView tv = new TextView(MainActivity.this); tv.setText(device.getName() + "----" + device.getAddress()); tv.setTextSize(25); tv.setTag(device);// 把textView和一个对象绑定起来 tv.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { BluetoothSocket socket; try { // // 这个是接受一个UUID, // 但是在蓝牙2.0的时候,UUID不重要.就写这个这么一个特殊的UUID就可以了 // 在蓝牙4.0的时候,UUID就重要了 socket = device.createRfcommSocketToServiceRecord(UUID .fromString("00001000-0000-1000-8000-00805F9B34FB}")); // 连接指令 socket.connect(); os = socket.getOutputStream(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); mLl_bluetooth.addView(tv); } } } @Override protected void onDestroy() { unregisterReceiver(mBluetoothReceiver); mBluetoothReceiver = null; } public void switchLight(View v) {// 开关电灯 try { os.write(0xA1); os.write(0xF1); os.flush(); } catch (IOException e) { e.printStackTrace(); } } public void scan(View v) { // 扫面发现蓝牙设备 mBluetoothAdapter.startDiscovery(); }}
需要的权限
<uses-permission android:name="android.permission.BLUETOOTH"/><uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
0 0
- android_蓝牙模块的开发
- android_蓝牙
- Android 端蓝牙模块的开发
- BM77SPP03蓝牙模块开发
- iOS开发-蓝牙模块
- android蓝牙开发-与蓝牙模块通信
- 蓝牙模块的使用
- 蓝牙模块的特性
- 蓝牙模块的测试
- 关于蓝牙串口模块开发
- 蓝牙、WIFI、无线模块的使用规范及开发指南
- Android_蓝牙详解一
- Android 蓝牙开发(一)Android系统的蓝牙模块简介
- Androidble4.0蓝牙开发兼容2.0蓝牙应用(针对arduino蓝牙控制小车开发应用HC-08,06蓝牙模块的连接)
- 蓝牙----Android的蓝牙开发
- 蓝牙模块开发音频音乐输出中,如何调大模块的音频输出?
- 蓝牙模块的一些版本
- 常见的蓝牙4.0模块
- JDK and Jre
- 六度空间 地下迷宫探索 哈利·波特的考试 旅游规划 畅通工程之最低成本建设问题
- 欢迎使用CSDN-markdown编辑器
- <Android Framework 之路>BootAnimation(1)
- Android学习之Activity与Service进行通信的三种方式
- android_蓝牙模块的开发
- 扩展欧几里得的学习(未完成
- java代码动态修改app图标
- Java中重写equals函数加判断自己定义的对象相不相等
- Oracle 大数据集成实施
- JS强制类型转换
- Python3.4中filter函数,map函数和reduce函数
- HDU 1242 - Rescue(BFS+优先队列)
- BestCoder Round #86题解报告