Skip to content

kingbeful/BLECommunication

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

BLECommunication

BLE (Bluetooth Low Energy) Central Lib for both Android and iOS. You can use this lib to connect to the BLE Peripheral(in our Example, it's OSX)

BLE has two modes Central and Peripheral

Android has supportted Central mode since android 4.3 (API 18). The Peripheral mode starts from android 5.0 with some special devices like Nexus 6 and Nexus 9.

Check this stackoverflow link for detail

In this library, both iOS and Android act in a central role while OSX acts in a peripheral role.

How to use

iOS

Add BLEUtil.h and BLEUtil.mm to your project. Import the header file

#import "BLEUtil.h"

Start the Bluetooth Central Manager

[[BLEUtil sharedInstance] startCentralManagerWithDelegate:self];

Scan the service UUID when Bluetooth is ready

- (bool)BLEUtilCentralEvent:(BLEUtilEvent) event withMessage:(NSString*) msg {
    if (event == EV_BT_POWER_ON) {
        [[BLEUtil sharedInstance] scanPeripheralWithServicesUUID:@"ADEF1234-7655-00EF-11EE-00B1AD073469"];
    }
    return YES;
}

Handle the callback in delegate

#pragma mark - BLE Delegate
- (bool)BLEUtilError:(BLEUtilErrorCode) code withMessage:(NSString*) msg {
    return YES;
}

- (bool)BLEUtilPeripheralEvent:(BLEUtilEvent) event withMessage:(NSString*) msg {
    return YES;
}

- (bool)BLEUtilCentralEvent:(BLEUtilEvent) event withMessage:(NSString*) msg {
  return YES;
}

Android

Add the BLEUtil.java to your project

Start the Bluetooth Central Manager with callback

BLEUtil.getInstance(this).startCentralManager(new BLEUtilCallback(){
	@Override
	public boolean BLEUtilErrorwithMessage(BLEUtilErrorCode code,
			String msg) {
		return true;
	}

	@Override
	public boolean BLEUtilCentralEventwithMessage(BLEUtilEvent event, String msg) {
		return true;
	}

	@Override
	public boolean BLEUtilPeripheralEventwithMessage(BLEUtilEvent event, String msg) {
		return true;
	}
	
});

Scan the service UUID when Bluetooth is ready

public boolean BLEUtilCentralEventwithMessage(BLEUtilEvent event, String msg) {
	if (event == BLEUtilEvent.EV_BT_POWER_ON) {
		if (Build.VERSION.SDK_INT >= 21) {
			BLEUtil.getInstance(MainActivity.this).scanPeripheralWithServicesUUID_API21("ADEF1234-7655-00EF-11EE-00B1AD073469");
		} else if (Build.VERSION.SDK_INT >= 18) {
			BLEUtil.getInstance(MainActivity.this).scanPeripheralWithServicesUUID_API18("ADEF1234-7655-00EF-11EE-00B1AD073469");
		}
	}
	return true;
}

Notice

There are two methods to scan the service depends on the api level.

Handle the callback

@Override
public boolean BLEUtilErrorwithMessage(BLEUtilErrorCode code, String msg) {
  // handle error message
	return true;
}

@Override
public boolean BLEUtilCentralEventwithMessage(BLEUtilEvent event, String msg) {
  // handle the Central event
	return true;
}

@Override
public boolean BLEUtilPeripheralEventwithMessage(BLEUtilEvent event, String msg) {
  // handle the Peripheral event
	return true;
}

About

BLE (Bluetooth Low Energy) Central Lib for both Android and iOS

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published