# MTSensorKit Documentation

​ This set of SDK only supports the Bluetooth Sensor device produced by Minew, and is currently a temperature and humidity sensor and a door sensor. The SDK can help developers handle all the work between mobile phones and Sensors, including: scanning devices, connecting devices, writing data to devices, receiving data from devices, etc.

# Preliminary work

​ Overall framework: MTCentralManager is a device management class, which is always a singleton when the APP is running. MTPeripheral is a device instance class. This suite will generate a MTPeripheral instance for each device to facilitate monitoring and operating devices.

MTCentralManager : Device management class, which can scan the surrounding Sensor devices, and can connect them, verify them, etc.

MTPeripheral : Device instance class. When MTCentralManager discovers a physical device, MTCentralManager will generate a MTPeripheral instance, which corresponds to a physical device.

MTBroadcastHandler : Device broadcast class, which can get the data when the device broadcasts.

MTConnectionHandler : Device connection class for receiving and sending data from the device.

MTUtils : Write data instruction.

# Get started

# Development environment:

-Xcode10 +, the current SDK is compiled with Xcode11, please use Xcode10 and above for development; -iOS11, the minimum system version is iOS11;

# Import into the project:
  1. CocoaPods

    MTSensorKit is available through CocoaPods (opens new window). To install it, simply add the following line to your Podfile, and then import <MTSensorKit/MTSensorKit.h>:

    pod 'MTSensorKit'
    
    1
  2. Manually

    • Copy the development kit files: MTSensorKit.framework files to the project project directory, and then add them to the project.

PS:

  1. !!! In iOS10 and above, Apple added permission restrictions on Bluetooth APi. You need to add a string to the project's info.plist file: Privacy-Bluetooth Peripheral Usage Description-"Your usage description".
  2. !!! In iOS13 and above, Apple added permission restrictions on Bluetooth APi. You need to add a string to the project's info.plist file: Privacy-Bluetooth Always Usage Description-"Your usage description".

# Start development

# Scanning equipment

​ First you need to get the singleton of MTCentralManager, then check the current Bluetooth status of the phone, and then you can scan the device.

// Get Manager singleton
MTCentralManager * manager = [MTCentralManager sharedInstance];

dispatch_after (dispatch_time (DISPATCH_TIME_NOW, (int64_t) (1 * NSEC_PER_SEC)), dispatch_get_main_queue (), ^ {
    // The current state of the Bluetooth switch on the mobile phone
    if (self-> manager.status == PowerStatePoweredOn) {
        // start device scan
        [manager startScan: ^ (NSArray <MTPeripheral *> * devices) {
        //According to the type of broadcast attribute, the required sensor type can be filtered.
        self-> deviceAry = devices;
    }];
    }
});
// Scanned devices can also be obtained using manager.scannedPeris
// If you need to respond to the Bluetooth status of your phone. Please listen for the callback.
[manager didChangesBluetoothStatus: ^ (PowerState status) {
  switch (status) {
    case PowerStatePoweredOn:
        NSLog (@ "bluetooth status change to poweron");
        break;
    case PowerStatePoweredOff:
        NSLog (@ "bluetooth status change to poweroff");
        break;
    case PowerStateUnknown:
        NSLog (@ "bluetooth status change to unknown");
	}
}];
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

PS: The entire SDK works only when the Bluetooth state of the phone is in Poweron.

# Connect to device
// The scanned device can be obtained from the previous step
MTPeripheral * device = deviceAry [0];
// Connect the device
[manager connectToPeriperal: device];
// Listen for device connection status.
[p.connector didChangeConnection: ^ (Connection connection) {
    if (connection == Vaildated) {
      // Successful verification, successfully connected to the device
        NSLog (@ "vaildated");
      //Need to write a password, perform other operations after the password is verified successfully
    }
    if (connection == Disconnected) {
        NSLog (@ "device has disconnected.");
    }
}];
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Write data to the device

​ Take the next step, when the mobile phone successfully establishes a connection with a device and the authentication is successful, the device can read and write.

  1. Write data

    //Obtain the corresponding command data through MTUtils, for example:
    NSData *data = [MTUtils verficationPassword:@"123456"];
    
    [self-> _ per.connector writeData: data completion: ^ (BOOL success, NSError * _Nonnull error) {
       if (error) {
           NSLog (@ "write data failed:% @", error);
       }
       else {
           NSLog (@ "write data success");
       }
    }];
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
  2. Receive data

    [_per.connector didReceiveData: ^ (NSData * _Nonnull data) {
       if (data) {
           NSLog (@ "receive data success:% @", data);
        }
        else {
           NSLog (@ "receive data failed");
        }
       }
    }];
    
    1
    2
    3
    4
    5
    6
    7
    8
    9

# Schedule

# MTCentralManager Property Description
Name Type Description
status PowerState phone's bluetooth status
scannedPeris NSArray scanned devices
# MTPeripheral Property Description
Name Type Description
identifier NSString device Identifier
broadcast MTBroadcastHandler obejct of MTBroadcastHandler
connector MTConnectionHandler obejct of MTConnectionHandler
# MTBroadcastHanler Property Description
Name Type Description
name NSString device name
rssi NSInteger device RSSI
battery NSString device battery
mac NSString device Mac
identifier NSString device identifier
temp double device temperature
humi double device humidity
reseInfo uint8_t device temperature unit flag
type SensorType device type
doorStatus NSString device door status
warningStatus uint8_t device alarm status
#
Name Type Description
name NSString device name
rssi NSInteger device RSSI
mac NSString device Mac
identifier NSString device identifier
firmVersion NSString firmversion
hardwareVersion NSString hardwareVersion
screenInfo NSString screenInfo
battery NSString battery
chipTemp double chipTemp
heartbeat NSString heartbeat
tagRssi NSString tagRssi
chipInfo NSString chipInfo
imageIdData NSData imageIdData
errorCode NSInteger errorCode
peripheralSupport NSString peripheralSupport
# MTConnectionHandler Property Description
Name Type Description
macString NSString device Mac
connection Connection device connect status
Last Updated:: 1/12/2024, 6:46:52 PM