Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BLE_API mbed nRF51822
main.cpp
00001 // ヘッダのインクルード 00002 #include "mbed.h" 00003 #include "BLEDevice.h" 00004 00005 #define CHARACTERISTIC_LEN 100 00006 00007 // デバイス名の登録 00008 const static char DEVICE_NAME[] = "mbed_HRM1017"; 00009 00010 // UUIDの登録 00011 static const uint8_t UUID_BRIL_SERVICE[] = {0x4d,0x92,0x37,0xc0,0xbd,0x5b,0x45,0x93,0xad,0x55,0xd8,0xf5,0x95,0xcf,0xe2,0xea}; 00012 static const uint8_t UUID_CHAR_DATA[] = {0xe5,0xc1,0xcf,0x6e,0xe0,0x57,0x40,0x08,0x98,0x21,0x17,0x71,0x10,0x24,0xe8,0x85}; 00013 00014 // 00015 uint8_t gRwData[CHARACTERISTIC_LEN] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; 00016 int g_conflg = 0; 00017 int g_test = 0; 00018 int g_receiveflg = 0; 00019 uint8_t g_txdata[100]; 00020 uint8_t g_rxdata[100]; 00021 00022 BLEDevice ble; 00023 Serial pc(USBTX, USBRX); 00024 00025 uint8_t txTempPayload[100]; 00026 00027 GattCharacteristic gDataCharacteristic ( UUID_CHAR_DATA, gRwData, sizeof(gRwData), sizeof(gRwData), 00028 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE); 00029 00030 GattCharacteristic txChar( 0x3A01, txTempPayload, 100, 100, 00031 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE | 00032 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | 00033 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE ); 00034 00035 // バッテリーレベル用変数 00036 uint8_t rxTempPayload[100]; 00037 00038 // バッテリーレベル用GATT 00039 GattCharacteristic rxChar( 0x3A02, rxTempPayload, 100, 100, 00040 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY | 00041 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | 00042 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE ); 00043 00044 // 00045 GattCharacteristic *txChars[] = {&txChar, }; 00046 GattCharacteristic *rxChars[] = {&rxChar, }; 00047 GattCharacteristic *RwDataChars[] = {&gDataCharacteristic}; 00048 00049 GattService txService( 0x181C, txChars, sizeof(txChars) / sizeof(GattCharacteristic * ) ); 00050 GattService rxService( 0x181C, rxChars, sizeof(rxChars) / sizeof(GattCharacteristic * ) ); 00051 00052 GattService gBrilService = GattService(UUID_BRIL_SERVICE, RwDataChars, sizeof(RwDataChars) / sizeof(GattCharacteristic *)); 00053 00054 uint16_t uuid16_list[] = { 0x181C, }; 00055 00056 static Gap::ConnectionParams_t connectionParams; 00057 00058 void onDisconnectionCallback( Gap::Handle_t handle, Gap::DisconnectionReason_t reason ){ 00059 00060 g_conflg = 0; 00061 ble.startAdvertising(); 00062 pc.printf( "Disconnect!!\r\n" ); 00063 00064 } 00065 00066 void onConnectionCallback( Gap::Handle_t handle, Gap::addr_type_t type, const Gap::address_t addr, 00067 Gap::addr_type_t addr_type_townAddrType, const Gap::address_t ownAddr, 00068 const Gap::ConnectionParams_t *params ){ 00069 00070 g_conflg = 1; 00071 pc.printf( "Connect!!\r\n" ); 00072 00073 } 00074 00075 void onDataWrittenCallback( const GattCharacteristicWriteCBParams *params ){ 00076 int i; 00077 00078 uint16_t bytesread = params->len; 00079 00080 pc.printf( "Written %d Bytes.\r\n", bytesread ); 00081 for( i = 0; i < bytesread; i++ ){ 00082 pc.printf( "%02x",params->data[i] ); 00083 if( ( i + 1 ) < bytesread ){ 00084 pc.printf( ", " ); 00085 } 00086 } 00087 pc.printf( "\r\n" ); 00088 00089 00090 ble.updateCharacteristicValue( txChar.getValueAttribute().getHandle(), params->data, bytesread ); 00091 00092 } 00093 00094 int main( void ) 00095 { 00096 00097 // 初期設定 00098 ble.init(); 00099 // コールバック関数の設定 00100 ble.onDisconnection( onDisconnectionCallback ); 00101 ble.onConnection( onConnectionCallback ); 00102 ble.onDataWritten(onDataWrittenCallback); 00103 00104 ble.getPreferredConnectionParams( &connectionParams ); 00105 00106 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); 00107 // ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t*)uuid16_list, sizeof(uuid16_list)); 00108 // ble.accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_THERMOMETER); 00109 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); 00110 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00111 ble.setAdvertisingInterval(160); // 100ms; in multiples of 0.625ms 00112 ble.setAdvertisingTimeout(0); // アドバタイジングモードのタイムアウト無効 00113 00114 // アドバタイズモード開始 00115 ble.startAdvertising(); 00116 00117 // サービスの追加 00118 ble.addService( txService ); 00119 ble.addService( rxService ); 00120 00121 while(1){ 00122 00123 ble.waitForEvent(); 00124 00125 } 00126 }
Generated on Tue Jul 12 2022 16:17:05 by
1.7.2