Android Bluetooth蓝牙开发(一)

想研究一下android蓝牙方面的开发,找来找去还是那几篇文章,能提供的信息也很少。想起之前接触的一个项目中,里面有一个连接传感器的模块。一直没研究它嫌麻烦,现在没办法只好硬着头皮去研究研究了。

那么进入正题,不管你是2个android手机连接蓝牙,还是android手机连接蓝牙模块,你终归是要先打开android自身的蓝牙,并搜索附近蓝牙设备的,完整的代码在最后面

1、相应的权限

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

2、打开蓝牙功能

...
private BluetoothAdapter m_BtAdapter;
...
在onCreate方法中
...
//获取默认的本地蓝牙适配器,参考①
m_BtAdapter = BluetoothAdapter.getDefaultAdapter();
//获取本地蓝牙适配器的状态,是否启用
if(!m_BtAdapter.isEnabled()){

    //如果没有启用,发出提示进行启用。
    Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(intent,REQUEST_ENABLE);
    //当然你也可以不提示,强行打开(如果没有root权限,系统会提示获取蓝牙的root权限)
    //m_BtAdapter.enable();
}

3、搜索蓝牙设备

  • 接受消息的监听
//接收消息的一个监听
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        //如果发现了一个蓝牙设备
        if (BluetoothDevice.ACTION_FOUND.equals(action)) {

            //我们拿到这个蓝牙设备
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            //打印出蓝牙的名称和蓝牙地址
            Log.i("info","devie 蓝牙名称:" +device.getName() + ",蓝牙地址:" + device.getAddress());

        }
        //搜索完成后
        else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {

            //打印搜索完成
            Log.i("info","devie : 搜索结束");
        }
    }
};
  • 注册监听

上面声明一个监听,那么我们要注册这个监听

//注册寻找设备的监听
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
this.registerReceiver(mReceiver, filter);
//注册寻找设备完成后的监听
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
this.registerReceiver(mReceiver, filter);
  • 开始搜索设备

监听写了,也注册了,最后我们启动这个注册的监听。

//如果当前本地蓝牙适配器处于搜索设备中
if(m_BtAdapter.isDiscovering()){
    //那么取消搜索
    m_BtAdapter.cancelDiscovery();
}
//开始搜索蓝牙设备
m_BtAdapter.startDiscovery();

运行结果如下:

10-28 14:20:54.686 4108-4108/? I/info: devie 蓝牙名称:xxxxx,蓝牙地址:C4:6A:B7:45:D5:BC
10-28 14:20:58.527 4108-4108/? I/info: devie 蓝牙名称:JN-YUNQIANG2014,蓝牙地址:90:48:9A:40:49:F6
10-28 14:21:01.720 4108-4108/? I/info: devie 蓝牙名称:Coolpad 8675-FHD,蓝牙地址:18:DC:56:00:62:D1
10-28 14:21:53.859 4108-4108/? I/info: devie 蓝牙名称:null,蓝牙地址:B8:BC:1B:10:6B:FA
10-28 14:22:04.367 4108-4108/? I/info: devie : 搜索结束

附上完整的代码:

package com.sangbo.bluetooth;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.sangb.projecttest.BaseActivity;
import com.sangb.projecttest.R;

/**
 * Created by 桑博 on 2015/10/28.
 */
public class BlueToothActivity extends BaseActivity{


    private Button m_btnFindDevice;
    private BluetoothAdapter m_BtAdapter;
    private final int REQUEST_ENABLE = 1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_blue_tooth);
        initViews();

        //获取默认的本地蓝牙适配器,参考①
        m_BtAdapter = BluetoothAdapter.getDefaultAdapter();
        //获取本地蓝牙适配器的状态,是否启用
        if(!m_BtAdapter.isEnabled()){

            //如果没有启用,发出提示进行启用。
            Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(intent,REQUEST_ENABLE);
            //当然你也可以不提示,强行打开(如果没有root权限,系统会提示获取蓝牙的root权限)
            m_BtAdapter.enable();


        }


        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        this.registerReceiver(mReceiver, filter);
        filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        this.registerReceiver(mReceiver, filter);

        m_btnFindDevice = (Button) findViewById(R.id.btn_find_device);
        m_btnFindDevice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //如果当前本地蓝牙适配器处于搜索设备中
                if(m_BtAdapter.isDiscovering()){
                    //那么取消搜索
                    m_BtAdapter.cancelDiscovery();
                }
                //开始搜索蓝牙设备
                m_BtAdapter.startDiscovery();

            }
        });

    }

    @Override
    public void initViews(){

    }

    //接收消息的一个监听
    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            //如果发现了一个蓝牙设备
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {

                //我们拿到这个蓝牙设备
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                //打印出蓝牙的名称和蓝牙地址
                Log.i("info","devie 蓝牙名称:" +device.getName() + ",蓝牙地址:" + device.getAddress());

            }
            //搜索完成后
            else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {

                //打印搜索完成
                Log.i("info","devie : 搜索结束");
            }
        }
    };
}

①官方是这么写的

//英文不好,大体意思就是获取默认的本地蓝牙适配器,目前的Android只支持一个蓝牙适配器,但是API可以扩展到支持更多。
//return 默认的本地适配器,如果蓝牙空不支持此硬件平台(此硬件平台没有蓝牙功能)
public static synchronized BluetoothAdapter getDefaultAdapter ()

Added in API level 5
Get a handle to the default local Bluetooth adapter.

Currently Android only supports one Bluetooth adapter, but the API could be extended to support more. This will always return the default adapter.

Returns
the default local adapter, or null if Bluetooth is not supported on this hardware platform

转载请注明: SangBigYe:http://blog.csdn.net/cutelittlebo/article/details/49468545

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值