android_蓝牙模块的开发

这里写图片描述

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值