展会信息港展会大全

蓝牙通信-搜索附近的蓝牙设备
来源:互联网   发布日期:2015-09-28 16:01:48   浏览:2590次  

导读:与其他设备通信通信之前需要搜索周围的蓝牙设备。怎么搜索呢??1.如果数据中已经和某些蓝牙设备绑定,可以使用BluetoothAdapter.getBondedDevices();方法获得...

与其他设备通信通信之前需要搜索周围的蓝牙设备。

怎么搜索呢??

1.如果数据中已经和某些蓝牙设备绑定,可以使用BluetoothAdapter.getBondedDevices();方法获得已经绑定的蓝牙设备列表

2.搜索周围的蓝牙设备受用BluetoothAdapter.startDiscovery()方法

3.搜索到的蓝牙设备都是通过广播返回,so..。需要注册广播接收器来获得已经搜索到的蓝牙设备。

下面我们看一下demo:

我们在布局文件中放一个按钮和一个显示文本TextView。

我们点击Button时,开始搜索附近的蓝牙设备。将搜索到的蓝牙设备追加到TextView上,我们将绑定的和搜索到的都显示在TextView上。

布局文件:

[html]

<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"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

tools:context=".MainActivity" >

<Button

android:id="@+id/button_id"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:onClick="onClick_Search"

android:text="搜索蓝牙设备" />

<TextView

android:id="@+id/tvDevices"

android:layout_width="fill_parent"

android:layout_below="@+id/button_id"

android:layout_height="wrap_content" />

</RelativeLayout>

<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"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

tools:context=".MainActivity" >

<Button

android:id="@+id/button_id"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:onClick="onClick_Search"

android:text="搜索蓝牙设备" />

<TextView

android:id="@+id/tvDevices"

android:layout_width="fill_parent"

android:layout_below="@+id/button_id"

android:layout_height="wrap_content" />

</RelativeLayout>

JAVA文件:

[java]

package com.example.search_bluetooth_devices;

import java.util.Set;

import android.os.Bundle;

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.view.Menu;

import android.view.View;

import android.view.Window;

import android.widget.TextView;

public class MainActivity extends Activity {

private TextView mTextView;

private BluetoothAdapter mBluetoothAdapter;

private BroadcastReceiver mReceiver = new BroadcastReceiver() {

@Override

public void onReceive(Context context, Intent intent) {

// TODO Auto-generated method stub

String action = intent.getAction();

// 获得已经搜索到的蓝牙设备

if (action.equals(BluetoothDevice.ACTION_FOUND)) {

BluetoothDevice device = intent

.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

// 搜索到的不是已经绑定的蓝牙设备

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

// 显示在TextView上

mTextView.append(device.getName() + ":"

+ device.getAddress()+"\n");

}

// 搜索完成

} else if (action

.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) {

setProgressBarIndeterminateVisibility(false);

setTitle("搜索蓝牙设备");

}

}

};

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

setContentView(R.layout.activity_main);

mTextView = (TextView) findViewById(R.id.tvDevices);

mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

// 获取所有已经绑定的蓝牙设备

Set<BluetoothDevice> devices = mBluetoothAdapter.getBondedDevices();

if (devices.size() > 0) {

for (BluetoothDevice bluetoothDevice : devices) {

mTextView.append(bluetoothDevice.getName() + ":"

+ bluetoothDevice.getAddress() + "\n\n");

}

}

// 注册用以接收到已搜索到的蓝牙设备的receiver

IntentFilter mFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);

registerReceiver(mReceiver, mFilter);

// 注册搜索完时的receiver

mFilter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);

registerReceiver(mReceiver, mFilter);

}

@Override

protected void onDestroy() {

// TODO Auto-generated method stub

super.onDestroy();

//解除注册

unregisterReceiver(mReceiver);

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

public void onClick_Search(View v) {

setProgressBarIndeterminateVisibility(true);

setTitle("正在扫描....");

// 如果正在搜索,就先取消搜索

if (mBluetoothAdapter.isDiscovering()) {

mBluetoothAdapter.cancelDiscovery();

}

// 开始搜索蓝牙设备,搜索到的蓝牙设备通过广播返回

mBluetoothAdapter.startDiscovery();

}

}

package com.example.search_bluetooth_devices;

import java.util.Set;

import android.os.Bundle;

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.view.Menu;

import android.view.View;

import android.view.Window;

import android.widget.TextView;

public class MainActivity extends Activity {

private TextView mTextView;

private BluetoothAdapter mBluetoothAdapter;

private BroadcastReceiver mReceiver = new BroadcastReceiver() {

@Override

public void onReceive(Context context, Intent intent) {

// TODO Auto-generated method stub

String action = intent.getAction();

// 获得已经搜索到的蓝牙设备

if (action.equals(BluetoothDevice.ACTION_FOUND)) {

BluetoothDevice device = intent

.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

// 搜索到的不是已经绑定的蓝牙设备

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

// 显示在TextView上

mTextView.append(device.getName() + ":"

+ device.getAddress()+"\n");

}

// 搜索完成

} else if (action

.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) {

setProgressBarIndeterminateVisibility(false);

setTitle("搜索蓝牙设备");

}

}

};

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

setContentView(R.layout.activity_main);

mTextView = (TextView) findViewById(R.id.tvDevices);

mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

// 获取所有已经绑定的蓝牙设备

Set<BluetoothDevice> devices = mBluetoothAdapter.getBondedDevices();

if (devices.size() > 0) {

for (BluetoothDevice bluetoothDevice : devices) {

mTextView.append(bluetoothDevice.getName() + ":"

+ bluetoothDevice.getAddress() + "\n\n");

}

}

// 注册用以接收到已搜索到的蓝牙设备的receiver

IntentFilter mFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);

registerReceiver(mReceiver, mFilter);

// 注册搜索完时的receiver

mFilter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);

registerReceiver(mReceiver, mFilter);

}

@Override

protected void onDestroy() {

// TODO Auto-generated method stub

super.onDestroy();

//解除注册

unregisterReceiver(mReceiver);

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

public void onClick_Search(View v) {

setProgressBarIndeterminateVisibility(true);

setTitle("正在扫描....");

// 如果正在搜索,就先取消搜索

if (mBluetoothAdapter.isDiscovering()) {

mBluetoothAdapter.cancelDiscovery();

}

// 开始搜索蓝牙设备,搜索到的蓝牙设备通过广播返回

mBluetoothAdapter.startDiscovery();

}

}

配置文件:

[html] view plaincopyprint?<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.example.search_bluetooth_devices"

android:versionCode="1"

android:versionName="1.0" >

<uses-sdk

android:minSdkVersion="8"

android:targetSdkVersion="17" />

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

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

<application

android:allowBackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

<activity

android:name="com.example.search_bluetooth_devices.MainActivity"

android:label="@string/app_name" >

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

</application>

</manifest>

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.example.search_bluetooth_devices"

android:versionCode="1"

android:versionName="1.0" >

<uses-sdk

android:minSdkVersion="8"

android:targetSdkVersion="17" />

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

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

<application

android:allowBackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

<activity

android:name="com.example.search_bluetooth_devices.MainActivity"

android:label="@string/app_name" >

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

</application>

</manifest>

赞助本站

人工智能实验室

相关热词: android开发 教程

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港