android蓝牙测试

1.添加权限

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

2.main.xml

<RelativeLayout 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"
    tools:context="com.example.bluetoothtest.MainActivity" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>

</RelativeLayout>

3.MainActivity.java

package com.example.bluetoothtest;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import android.app.Activity;
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.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {
    private BluetoothAdapter bluetoothAdapter = BluetoothAdapter
            .getDefaultAdapter();
    private List<BluetoothDevice> deviceList = new ArrayList<BluetoothDevice>();
    private int REQUEST_ENABLE_BT = 1;

    private ListView listView;
    private ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        listView = (ListView)findViewById(R.id.listView1);
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
        listView.setAdapter(adapter);

        if (bluetoothAdapter == null) {
            Toast.makeText(getApplicationContext(), "bluetooth not exist!",
                    Toast.LENGTH_SHORT).show();
            finish();
        } else if (!bluetoothAdapter.isEnabled()) {
            // Show a system activity that allows the user to turn on Bluetooth
            Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(intent, REQUEST_ENABLE_BT);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        // 注册接收器
        Intent discoverIntent = new Intent(
                BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
        // 开启搜索看见模式,最长10s
        discoverIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,
                10);
        startActivity(discoverIntent);

        IntentFilter discoverFilter = new IntentFilter(
                BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        registerReceiver(discover_R, discoverFilter);
        IntentFilter findFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        registerReceiver(find_R, findFilter);

        bluetoothAdapter.startDiscovery();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (bluetoothAdapter != null) {
            bluetoothAdapter.cancelDiscovery();
            bluetoothAdapter.disable();
        }
    }

    // ACTION_DISCOVERY_FINISHED,搜索结束后追加已配对的设备
    // getName()这一步比较漫长,“已配对”那句话要等好一会儿才出现
    BroadcastReceiver discover_R = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            getName();
            unregisterReceiver(find_R);
            unregisterReceiver(this);
        }
    };

    // ACTION_FOUND,这是搜索到的所有蓝牙(未配对+已配对)
    BroadcastReceiver find_R = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            deviceList.add(device);
            adapter.add(device.getName());
        }
    };

    private void getName() {
        Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
        if(pairedDevices.size() > 0){
            for(BluetoothDevice device : pairedDevices){
                deviceList.add(device);
                adapter.add(device.getName()+"已配对!");
            }
        }
    }
}

注意点:在执行getBondedDevices时候会花费一点时间,一开始我不知道,“已配对”一直没出现,以为出错了。后来上了趟厕所回来发现就有了!!!!尴尬!

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券