Git Product home page Git Product logo

bluetoothcommon's Introduction

BluetoothCommon

首先要在Application的onCreate中初始化BluetoothManager.

public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        BluetoothManager.init(this);
    }
}

一、蓝牙扫描

支持Bluetooth LE和BluetoothClassic综合扫描,可灵活配置扫描策略。 如果启动扫描任务时,之前的扫描任务还未完成,则会自动取消之前的任务。

BluetoothSearchRequest request = new BluetoothSearchRequest.Builder()
        .searchBluetoothLeDevice(10000)  // 先扫描BLE 10s
        .searchBluetoothClassicDevice(5000) // 然后扫描classic 5s
        .multiSearchBluetoothLeDevice(1000, 30) // 然后扫描BLE 30次,每次1s
        .multiSearchBluetoothClassicDevice(1000, 20) // 最后扫描classic 20次,每次1s
        .build();

BluetoothSearchHelper.getInstance().startSearch(request, new BluetoothSearchResponse() {
    @Override
    public void onSearchStarted() {

    }

    @Override
    public void onDeviceFounded(XmBluetoothDevice device) {

    }

    @Override
    public void onSearchStopped() {

    }

    @Override
    public void onSearchCanceled() {

    }
});

// 取消扫描任务
BluetoothSearchHelper.getInstance().cancelSearch(request);

二、连接

BLEConnectManager.connect(mDeviceMac, new BleConnectResponse() {

    @Override
    public void onResponse(int code, Bundle data) {
        if (code == Code.REQUEST_SUCCESS) {

        } else {
            
        }
    }
});

BLEConnectManager.disconnect(mDeviceMac);

三、数据读写

BLEConnectManager.read(mDeviceMac, serviceUUID, characterUUID, new BleReadResponse() {

    @Override
    public void onResponse(int code, byte[] data) {
        if (code == Code.REQUEST_SUCCESS) {

        } else {

        }
    }
});

byte[] bytes = new byte[] { 0x1, 0x3 };
        
BLEConnectManager.write(mDeviceMac, serviceUUID, characterUUID, bytes, new BleWriteResponse() {

    @Override
    public void onResponse(int code, Void data) {
        if (code == Code.REQUEST_SUCCESS) {

        } else {

        }
    }
});

四、打开通知

BLEConnectManager.notify(mDeviceMac, serviceUUID, characterUUID, new BleNotifyResponse() {

    @Override
    public void onResponse(int code, Void data) {
        if (code == Code.REQUEST_SUCCESS) {

        } else {

        }
    }
});

BLEConnectManager.unnotify(mDeviceMac, serviceUUID, characterUUID);

五、读取RSSI

BLEConnectManager.readRemoteRssi(mDeviceMac, new BleReadRssiResponse() {
    @Override
    public void onResponse(int code, Integer rssi) {
        if (code == Code.REQUEST_SUCCESS) {

        } else {

        }
    }
});

六、状态通知

状态通知包括蓝牙连接状态变化和notify通知,两者都是通过BroadcastReceiver实现的,如果要关注相关的状态,则需要注册Receiver,如下:

IntentFilter filter = new IntentFilter(XmBluetoothManager.ACTION_CHARACTER_CHANGED);
filter.addAction(XmBluetoothManager.ACTION_CONNECT_STATUS_CHANGED);
LocalBroadcastManager.getInstance(context).registerReceiver(mReceiver, filter);

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {

	@Override
	public void onReceive(Context context, Intent intent) {
		if (intent == null) {
			return;
		}

		String mac = intent
				.getStringExtra(XmBluetoothManager.KEY_DEVICE_ADDRESS);

		String action = intent.getAction();

		if (XmBluetoothManager.ACTION_CHARACTER_CHANGED
				.equalsIgnoreCase(action)) {
			UUID service = (UUID) intent
					.getSerializableExtra(XmBluetoothManager.KEY_SERVICE_UUID);
			UUID character = (UUID) intent
					.getSerializableExtra(XmBluetoothManager.KEY_CHARACTER_UUID);
			byte[] value = intent
					.getByteArrayExtra(XmBluetoothManager.KEY_CHARACTER_VALUE);

			if (service != null && character != null) {
				processNotify(service, character, value);
			}
		} else if (XmBluetoothManager.ACTION_CONNECT_STATUS_CHANGED
				.equalsIgnoreCase(action)) {
			int status = intent.getIntExtra(
					XmBluetoothManager.KEY_CONNECT_STATUS,
					XmBluetoothManager.STATUS_UNKNOWN);
			processConnectStatusChanged(status);
		}
	}
};

bluetoothcommon's People

Contributors

dingjikerbo avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.