蓝牙应用入门

概述:

  安卓平台包括支持蓝牙网络协议栈,他允许设备与其他蓝牙设备无线交换数据。应用程序框架提供了安卓蓝牙接口访问蓝牙的功能,这些应用程序无线连接到其他蓝牙设备,实现点对点或点对多点的功能。

  对于低功耗的蓝牙设备,在Android4.3(API18)有支持

权限说明:

<span style="font-size:14px;">    <!-- 任意蓝牙的交互如:请求连接、接受连接、传输数据都需要此权限-->
    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <!-- 让蓝牙设备可见、操作蓝牙设置需要此权限,当需要此权限时必须加上上面的权限-->
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/></span>
下面是对蓝牙的一些基本操作:

public class BlueToothOp {
    private BluetoothAdapter adapter;

    public BlueToothOp() {
        adapter = BluetoothAdapter.getDefaultAdapter();
    }

    //检测蓝牙是否存在
    public boolean isExist() {
        if (adapter == null)
            return false;
        return true;
    }

    //检测蓝牙是否可用
    public boolean isEnable() {
        return adapter.isEnabled();
    }

    //蓝牙是否可见
    public boolean isCover() {
        return adapter.isDiscovering();
    }

    //打开蓝牙
    public void open(Activity activity, int requestCode) {
        Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        activity.startActivityForResult(intent, requestCode);
    }

    //设置蓝牙设备可见
    public void openDiscover(Activity activity, int requestCode) {
        Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
        intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
        activity.startActivityForResult(intent, requestCode);
    }

    //关闭蓝牙
    public void close() {
        adapter.disable();
    }
}
回调方法:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 0 && resultCode == RESULT_OK){
            showToast("打开蓝牙成功");
        }
        if(requestCode == 1 && resultCode == RESULT_OK){
            showToast("打开蓝牙失败");
        }
        //当你选择取消时候resultCode返回RESULT_CANCELED
    }
当然你的应用也可以监听 ACTION_STATE_CHANGED,需要注册广播:

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        registerReceiver(receiver,intentFilter);

    }

    BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            int action = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,-1);
            switch (action){
                case BluetoothAdapter.STATE_TURNING_ON:
                    System.out.println("正在打开");
                    break;
                case BluetoothAdapter.STATE_ON:
                    System.out.println("打开完毕");
                    break;
                case BluetoothAdapter.STATE_TURNING_OFF:
                    System.out.println("正在关闭");
                    break;
                case BluetoothAdapter.STATE_OFF:
                    System.out.println("关闭完毕");
                    break;
                case BluetoothAdapter.STATE_CONNECTED://连接
                    System.out.println("连接完毕");
                    break;
                case BluetoothAdapter.STATE_CONNECTING://正在连接
                    System.out.println("正在连接");
                    break;
            }
        }
    };
下载地址:http://download.csdn.net/detail/weiwozhiyi/9453208

你可能感兴趣的:(android)