bluetooth - muyu01248/lypk GitHub Wiki

private BluetoothAdapter mBluetoothAdapter; private static final int REQUEST_CODE=2;

mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

if (mBluetoothAdapter == null) { Toast.makeText(this, "此设备不支持蓝牙", Toast.LENGTH_SHORT).show(); this.finish(); }

if (!mBluetoothAdapter.isEnabled()) { // 弹出对话框提示用户是后打开 Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(intent, REQUEST_CODE); }

Set pList=mBluetoothAdapter.getBondedDevices(); if(pList!=null&&pList.size()>0){ for (BluetoothDevice bluetoothDevice : pList) { tv_devices.append(bluetoothDevice.getName()+":"+bluetoothDevice.getAddress()+":"+bluetoothDevice.getBondState()); Log.i("已配对设备", tv_devices.getText().toString()); } }

IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);

registerReceiver(receiver, filter);

filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);

registerReceiver(receiver, filter);

setProgressBarIndeterminateVisibility(true);

if (mBluetoothAdapter.isDiscovering()) { mBluetoothAdapter.cancelDiscovery(); }

mBluetoothAdapter.startDiscovery();

private final BroadcastReceiver receiver = 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);

    if (device.getBondState() != BluetoothDevice.BOND_BONDED) {

        tv_devices.append(device.getName() + ":" + device.getAddress() + "\n");
        Log.i("tag", tv_devices.getText().toString());
    }

} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {

    setProgressBarIndeterminateVisibility(true);

}
 }

};

BluetoothDevice:: int BOND_BONDED 表明蓝牙已经绑定 int BOND_BONDING 表明蓝牙正在绑定过程中 , bounding int BOND_NONE 表明没有绑定

BroadcastReceiver:: ACTION_STATE_CHANGED 蓝牙状态值发生改变 ACTION_SCAN_MODE_CHANGED 蓝牙扫描状态(SCAN_MODE)发生改变 ACTION_DISCOVERY_STARTED 蓝牙扫描过程开始 ACTION_DISCOVERY_FINISHED 蓝牙扫描过程结束 ACTION_LOCAL_NAME_CHANGED 蓝牙设备Name发生改变 ACTION_REQUEST_DISCOVERABLE 请求用户选择是否使该蓝牙能被扫描 PS:如果蓝牙没有开启,用户点击确定后,会首先开启蓝牙,继而设置蓝牙能被扫描。 ACTION_REQUEST_ENABLE 请求用户选择是否打开蓝牙 ACTION_FOUND

public String getName () 功能:获取蓝牙设备Name public String getAddress () 功能:获取蓝牙设备的硬件地址(MAC地址),例如:00:11:22:AA:BB:CC
public boolean setName (String name) 功能:设置蓝牙设备的Name,

public Set getBondedDevices () 功能:获取与本机蓝牙所有绑定的远程蓝牙信息,以BluetoothDevice类实例(稍后讲到)返回。 注意:如果蓝牙为开启,该函数会返回一个空集合 。

public static boolean checkBluetoothAddress (String address) 功能: 验证蓝牙设备MAC地址是否有效。所有设备地址的英文字母必须大写,48位,形如:00:43:A8:23:10:F1 。 返回值: true 设备地址有效 false 设备地址无效

public BluetoothDevice getRemoteDevice (String address) 功能:以给定的MAC地址去创建一个 BluetoothDevice 类实例(代表远程蓝牙实例)。即使该蓝牙地址不可见,也会产生 一个BluetoothDevice 类实例。 返回:BluetoothDevice 类实例 。注意,如果该蓝牙设备MAC地址不能被识别,其蓝牙Name为null。 异常:如果MAC address无效,抛出IllegalArgumentException。

public boolean startDiscovery () 功能: 扫描蓝牙设备 注意: 如果蓝牙没有开启,该方法会返回false ,即不会开始扫描过程。

public boolean cancelDiscovery () 功能: 取消扫描过程。 注意: 如果蓝牙没有开启,该方法会返回false。

public boolean isDiscovering () 功能: 是否正在处于扫描过程中。 注意: 如果蓝牙没有开启,该方法会返回false。

public boolean disable () 功能:关闭蓝牙设备。 返回值:该函数会立即返回。 true 表示关闭操作成功 false 表示蓝牙操作失败 , ①、当前蓝牙已经关闭 ; ②、其他一些异常情况

//第一种打开方法: 调用enable 即可
boolean result = mBluetoothAdapter.enable();

//
/第二种打开方法 ,调用系统API去打开蓝牙
if (!mBluetoothAdapter.isEnabled()) //未打开蓝牙,才需要打开蓝牙
{
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent, REQUEST_OPEN_BT_CODE);
//会以Dialog样式显示一个Activity , 我们可以在onActivityResult()方法去处理返回值
}

BluetoothAdapter SCAN_MOD状态值 ,即扫描状态 首先说明,可以扫描其他设备的,当然它同时能被其他蓝牙设备扫码。

int SCAN_MODE_CONNECTABLE 表明该蓝牙可以扫描其他蓝牙设备 int SCAN_MODE_CONNECTABLE_DISCOVERABLE
表 明该蓝牙设备同时可以扫码其他蓝牙设备,并且可以被其他蓝牙设备扫描到。 int SCAN_MODE_NONE : 该蓝牙不能扫描以及被扫描。

public static synchronized BluetoothAdapter getDefaultAdapter () 功能:获得本设备的蓝牙适配器实例。 返回值:如果设备具备蓝牙功能,返回BluetoothAdapter 实例;否则,返回null对象。

BluetoothAdapter STATE 状态值 , 即开关状态

          int STATE_OFF        蓝牙已经关闭
          int STATE_ON        蓝牙已经打开
          int STATE_TURNING_OFF      蓝牙处于关闭过程中 ,关闭ing
          int STATE_TURNING_ON        蓝牙处于打开过程中 ,打开ing