Giter VIP home page Giter VIP logo

android-bleperipheral's Introduction

Android-BLEPeripheral

android 从4.3系统开始可以连接BLE设备,iOS是从7.0版本开始支持BLE

android 从5.0系统开始可以模拟设备发出BLE广播,这个新功能是对标于iOS系统的手机模拟iBeacon设备

BLE设备之所以能被手机扫描到,是因为BLE设备在每隔一段时间广播一次,这个广播里面包含很多数据

1.检查是否支持BLE、BLE Peripheral

    /**
     * 首先检查是否支持BLE、BLE Peripheral
     */
    private void init() {
        if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
            Toast.makeText(this, "BLE not supported", Toast.LENGTH_LONG).show();
//            finish();
        }

        mBluetoothManager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
        mBluetoothAdapter = mBluetoothManager.getAdapter();

        if (mBluetoothAdapter == null) {
            Toast.makeText(this, "Bluetooth not supported", Toast.LENGTH_LONG).show();
        }

        if (!mBluetoothAdapter.isEnabled()) {
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
            //判断是否打开蓝牙,如果没有打开需要回调打开蓝牙,这里省略,请自行打开蓝牙
        }

        if (!mBluetoothAdapter.isMultipleAdvertisementSupported()) {
            Toast.makeText(this, "This device not support peripheral", Toast.LENGTH_SHORT).show();
        }

        mBluetoothLeAdvertiser = mBluetoothAdapter.getBluetoothLeAdvertiser();
        if (mBluetoothLeAdvertiser == null) {
            Toast.makeText(this, "This device not support peripheral", Toast.LENGTH_SHORT).show();
        }
    }

2.准备发送广播

发送广播

mBluetoothLeAdvertiser.startAdvertising(createAdvSettings(true, 0), createAdvertiseData(), mAdvertiseCallback);

广播设置参数AdvertiseSettings

/** create AdvertiseSettings */
		 public static AdvertiseSettings createAdvSettings(boolean connectable, int timeoutMillis) {
			 AdvertiseSettings.Builder mSettingsbuilder = new AdvertiseSettings.Builder();
			 mSettingsbuilder.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_BALANCED);
			 mSettingsbuilder.setConnectable(connectable);
			 mSettingsbuilder.setTimeout(timeoutMillis);
			 mSettingsbuilder.setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH);
			 AdvertiseSettings mAdvertiseSettings = mSettingsbuilder.build();
				if(mAdvertiseSettings == null){
					if(D){
						Toast.makeText(mContext, "mAdvertiseSettings == null", Toast.LENGTH_LONG).show();
						Log.e(TAG,"mAdvertiseSettings == null");
					}
				}
			return mAdvertiseSettings;
		 }

广播数据AdvertiseData

public static AdvertiseData createAdvertiseData(){		 
		 	AdvertiseData.Builder    mDataBuilder = new AdvertiseData.Builder();
			mDataBuilder.addServiceUuid(ParcelUuid.fromString(HEART_RATE_SERVICE));
			AdvertiseData mAdvertiseData = mDataBuilder.build();
			if(mAdvertiseData==null){
				if(D){
					Toast.makeText(mContext, "mAdvertiseSettings == null", Toast.LENGTH_LONG).show();
					Log.e(TAG,"mAdvertiseSettings == null");
				}
			}	
			return mAdvertiseData;
	 }

广播回调Callback

private AdvertiseCallback mAdvertiseCallback = new AdvertiseCallback() {
		@Override
		  public void onStartSuccess(AdvertiseSettings settingsInEffect) {
			super.onStartSuccess(settingsInEffect);
			 if (settingsInEffect != null) {
				 Log.d(TAG, "onStartSuccess TxPowerLv=" + settingsInEffect.getTxPowerLevel()	 + " mode=" + settingsInEffect.getMode()
				 + " timeout=" + settingsInEffect.getTimeout());
				 } else {
				 Log.e(TAG, "onStartSuccess, settingInEffect is null");
				 }
				showText("1. initGattServer Success");
			 	Log.e(TAG,"onStartSuccess settingsInEffect" + settingsInEffect);
		    }
		
		@Override
		public void onStartFailure(int errorCode) {
			super.onStartFailure(errorCode);
			showText("1. initGattServer failed");
	    }
	};

广播关闭

private void stopAdvertise() {
			 if (mBluetoothLeAdvertiser != null) {
				 mBluetoothLeAdvertiser.stopAdvertising(mAdvertiseCallback);
				 mBluetoothLeAdvertiser = null;
			 }
		 }

android-bleperipheral's People

Contributors

xmaihh avatar

Stargazers

 avatar

Watchers

 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.