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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 上海医保本丢了怎么办? 新版医保卡丢了怎么办 武汉社保卡掉了怎么办 职工社保卡丢了怎么办 杭州社保卡丢了怎么办 农村医疗卡丢了怎么办 陕西省医保卡丢了怎么办 小孩社保卡掉了怎么办 社区医保本丢了怎么办 宝宝医保卡掉了怎么办 同煤医疗卡丢了怎么办 杭州医保卡丢了怎么办 新的医保卡丢了怎么办 二代医保卡丢了怎么办 老医保卡丢了怎么办 上海医保卡余额用完了怎么办 身份证丢了医疗报销怎么办 取公积金身份证丢了怎么办 身份证丢了怎么办就诊卡 人在外地怎么办农村社保卡 武汉医保卡丢了怎么办 济宁社保卡丢了怎么办 农村医疗本丢了怎么办 常熟医保卡丢了怎么办 农民社保卡丢了怎么办 常熟社保卡坏了怎么办 社保卡丢失补办期看病怎么办 社保卡补办期间看病怎么办 医保卡冻结了出院结算怎么办 住院医保卡钱不够怎么办 住院押金条丢了怎么办 急用新社保卡要怎么办 看病没带社保卡怎么办 医保卡掉了住院怎么办 厦门医保卡丢了怎么办 成都医保卡丢了怎么办 长春医保卡丢了怎么办 县城医保卡丢了怎么办 医保卡丢了怎么办南宁 西安职工医保丢了怎么办 重庆医保卡丢了怎么办