Android 蓝牙操作--读取远程已配对的蓝牙设备

1》在AndroidManifest.xml文件中添加相关权限

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.myapp.bluetoothdemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <!-- 1.添加蓝牙使用权限 -->
    <uses-permission android:name="android.permission.BLUETOOTH" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".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>
2》获取信息,具体详见代码注示2,3,4,5

package com.myapp.bluetoothdemo;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import android.support.v7.app.ActionBarActivity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.os.RemoteCallbackList;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends ActionBarActivity {

	// 声明控件
	private Button scanBlueDev_btn;

	// 本地蓝牙设备
	private BluetoothAdapter localBlueTooth;

	// 与本地蓝牙设备配对的远程蓝牙设备
	private Set<BluetoothDevice> remoteBlueToothes;

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

	private void init() {
		scanBlueDev_btn = (Button) findViewById(R.id.scanBlueDev_btn);
		scanBlueDev_btn.setOnClickListener(blueDevClickListener);
	}

	OnClickListener blueDevClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.scanBlueDev_btn:

				scanBlueDevices();
				break;

			default:
				break;
			}
		}
	};

	private void scanBlueDevices() {
		// 2.得到本地蓝牙设备
		localBlueTooth = BluetoothAdapter.getDefaultAdapter();
		// 3. 判断本地是否有蓝牙设备
		if (localBlueTooth != null) {// 本地有蓝牙设备

			// 4.判断本地蓝牙设备是否已经打开
			if (!localBlueTooth.isEnabled()) {// 本地蓝牙未打开,提示打开蓝牙

				Intent blueToothIntent = new Intent(
						BluetoothAdapter.ACTION_REQUEST_ENABLE);
				startActivity(blueToothIntent);
			}
			// 5.获取已配对的远程蓝牙设备
			// 注意:即使本地蓝牙未打开,也可以得到已配对的远程蓝牙设备,因为这些信息已经存储在本地了。
			remoteBlueToothes = localBlueTooth.getBondedDevices();
			if (remoteBlueToothes.size() > 0) {
				for (Iterator iterator = remoteBlueToothes.iterator(); iterator
						.hasNext();) {
					BluetoothDevice bluetoothDevice = (BluetoothDevice) iterator
							.next();
					Log.i("CXC", bluetoothDevice.getAddress());
				}

			}

		} else {// 本地没有蓝牙设备
			Log.i("CXC", "本地没有蓝牙设备");
		}

	}

	@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;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值