android编写蓝牙服务端,Android Bluetooth蓝牙客户端发起对服务端连接建立请求过程(高版本Android兼容)详解...

Android Bluetooth蓝牙客户端发起对服务端连接建立请求过程(高版本Android兼容)

本例代码是蓝牙客户端代码,只完成连接建立请求,假定蓝牙服务端设备名是:Android-Phone。兼容高版本Android系统。低版本则无需运行时权限申请。package zhangphil.client;

import java.util.UUID;

import android.Manifest;

import android.app.Activity;

import android.bluetooth.BluetoothAdapter;

import android.bluetooth.BluetoothDevice;

import android.bluetooth.BluetoothSocket;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.content.IntentFilter;

import android.content.pm.PackageManager;

import android.os.Build;

import android.os.Bundle;

import android.text.TextUtils;

import android.util.Log;

public class MainActivity extends Activity {

private BluetoothAdapter mBluetoothAdapter;

private final int PERMISSION_REQUEST_COARSE_LOCATION = 0xb01;

private String TAG = "zhangphil";

private String MY_UUID = "00001101-0000-1000-8000-00805F9B34FB";

// 广播接收发现蓝牙设备

private 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);

if (device != null) {

String name = device.getName();

Log.d(TAG, "发现设备:" + name);

if (TextUtils.equals(name, "Android-Phone")) {

Log.d(TAG, "发现目标设备,开始线程连接!");

// 蓝牙搜索是非常消耗系统资源开销的过程.

// 一旦发现了目标感兴趣的设备,可以考虑关闭扫描。

mBluetoothAdapter.cancelDiscovery();

new ClientThread(device).start();

}

}

}

}

};

private class ClientThread extends Thread {

private BluetoothDevice device;

public ClientThread(BluetoothDevice device) {

this.device = device;

}

@Override

public void run() {

BluetoothSocket socket;

try {

socket = device.createRfcommSocketToServiceRecord(UUID.fromString(MY_UUID));

Log.d(TAG, "连接服务端...");

socket.connect();

Log.d(TAG, "连接建立.");

} catch (Exception e) {

e.printStackTrace();

}

}

}

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

if (this.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_COARSE_LOCATION);

}

}

mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

// 注册广播接收器。接收蓝牙发现讯息

IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);

registerReceiver(mReceiver, filter);

if (mBluetoothAdapter.startDiscovery()) {

Log.d(TAG, "启动蓝牙扫描设备...");

}

}

@Override

protected void onDestroy() {

super.onDestroy();

unregisterReceiver(mReceiver);

}

@Override

public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {

switch (requestCode) {

case PERMISSION_REQUEST_COARSE_LOCATION:

if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {

}

break;

}

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值