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 #include "ble_gap.h" 00005 #include "Gap.h" 00006 00007 #define CHARACTERISTIC_LEN 100 00008 00009 // デバイス名の登録 00010 const static char DEVICE_NAME[] = "mbed_HRM1017"; 00011 00012 // UUIDの登録 00013 static const uint8_t UUID_BRIL_SERVICE[] = {0x4d,0x92,0x37,0xc0,0xbd,0x5b,0x45,0x93,0xad,0x55,0xd8,0xf5,0x95,0xcf,0xe2,0xea}; 00014 static const uint8_t UUID_CHAR_DATA[] = {0xe5,0xc1,0xcf,0x6e,0xe0,0x57,0x40,0x08,0x98,0x21,0x17,0x71,0x10,0x24,0xe8,0x85}; 00015 00016 // ble_gap_evt_scan_req_report_t report_rssi; 00017 // 00018 uint8_t gRwData[CHARACTERISTIC_LEN] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; 00019 uint8_t g_txdata[100]; 00020 uint8_t g_rxdata[100]; 00021 00022 BLEDevice ble; 00023 Serial pc(USBTX, USBRX); 00024 00025 GattCharacteristic gDataCharacteristic ( UUID_CHAR_DATA, gRwData, sizeof(gRwData), sizeof(gRwData), 00026 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE); 00027 00028 // 送信用バッファ 00029 uint8_t txTempPayload[100]; 00030 00031 // タイマ送信用バッファ 00032 uint8_t txUpdate[5] = { 0, 0, 0, 0, 0 }; 00033 00034 // 送信用GATTの宣言 00035 GattCharacteristic txChar( 0x3A01, txTempPayload, 100, 100, 00036 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE | 00037 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | 00038 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE ); 00039 00040 // 受信用バッファ 00041 uint8_t rxTempPayload[100]; 00042 00043 // 受信用GATTの宣言 00044 GattCharacteristic rxChar( 0x3A02, rxTempPayload, 100, 100, 00045 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY | 00046 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | 00047 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE ); 00048 00049 // RSSI値用変数 00050 int8_t g_rssi; 00051 00052 // RSSI値用GATTの宣言 00053 GattCharacteristic rssiChar( 0x3A03, (uint8_t *)g_rssi, 1, 1, 00054 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY | 00055 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | 00056 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE ); 00057 00058 GattCharacteristic *txChars[] = {&txChar, }; 00059 GattCharacteristic *rxChars[] = {&rxChar, }; 00060 GattCharacteristic *rssiChars[] = {&rssiChar, }; 00061 GattCharacteristic *RwDataChars[] = {&gDataCharacteristic}; 00062 00063 GattService txService( 0x181C, txChars, sizeof(txChars) / sizeof( GattCharacteristic * ) ); 00064 GattService rxService( 0x181C, rxChars, sizeof(rxChars) / sizeof( GattCharacteristic * ) ); 00065 GattService rssiService( 0x181C, rssiChars, sizeof(rssiChars) / sizeof( GattCharacteristic * ) ); 00066 00067 GattService gBrilService = GattService(UUID_BRIL_SERVICE, RwDataChars, sizeof(RwDataChars) / sizeof(GattCharacteristic *)); 00068 00069 uint16_t uuid16_list[] = { 0x181C, }; 00070 00071 static Gap::ConnectionParams_t connectionParams; 00072 00073 void onDisconnectionCallback( Gap::Handle_t handle, Gap::DisconnectionReason_t reason ){ 00074 00075 ble.startAdvertising(); 00076 pc.printf( "Disconnect!!\r\n" ); 00077 00078 } 00079 00080 void onConnectionCallback( Gap::Handle_t handle, Gap::addr_type_t type, const Gap::address_t addr, 00081 Gap::addr_type_t addr_type_townAddrType, const Gap::address_t ownAddr, 00082 const Gap::ConnectionParams_t *params ){ 00083 00084 pc.printf( "Connect!!\r\n" ); 00085 00086 } 00087 00088 void onDataWrittenCallback( const GattCharacteristicWriteCBParams *params ){ 00089 int i; 00090 int8_t p_rssi=0; 00091 int error = 0; 00092 00093 uint16_t bytesread = params->len; 00094 00095 pc.printf( "Written: %d Bytes.\r\n", bytesread ); 00096 for( i = 0; i < bytesread; i++ ){ 00097 pc.printf( "%02x",params->data[i] ); 00098 if( ( i + 1 ) < bytesread ){ 00099 pc.printf( ", " ); 00100 } 00101 } 00102 00103 // ble.updateCharacteristicValue( txChar.getValueAttribute().getHandle(), params->data, bytesread ); 00104 00105 error = sd_ble_gap_rssi_get( rxChar.getValueAttribute().getHandle(), ( int8_t * )p_rssi ); 00106 sd_ble_gap_rssi_stop( rxChar.getValueAttribute().getHandle() ); 00107 sd_ble_gap_rssi_start( rxChar.getValueAttribute().getHandle(), 0x00, 0x00 ); 00108 00109 pc.printf( " RSSI: %d, ", p_rssi ); 00110 pc.printf( "ERROR: %d\r\n", error ); 00111 00112 ble.updateCharacteristicValue( rssiChar.getValueAttribute().getHandle(), (uint8_t *)p_rssi, sizeof( p_rssi ) ); 00113 00114 00115 } 00116 00117 void tickerCallback( void ){ 00118 uint16_t byteswrite = 1; 00119 00120 ble.updateCharacteristicValue( txChar.getValueAttribute().getHandle(), txUpdate, byteswrite ); 00121 00122 txUpdate[0]++; 00123 } 00124 00125 int main( void ) 00126 { 00127 00128 // 初期設定 00129 ble.init(); 00130 // コールバック関数の設定 00131 ble.onDisconnection( onDisconnectionCallback ); 00132 ble.onConnection( onConnectionCallback ); 00133 ble.onDataWritten(onDataWrittenCallback); 00134 00135 ble.getPreferredConnectionParams( &connectionParams ); 00136 00137 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); 00138 // ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t*)uuid16_list, sizeof(uuid16_list)); 00139 // ble.accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_THERMOMETER); 00140 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); 00141 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00142 ble.setAdvertisingInterval(160); // 100ms; in multiples of 0.625ms 00143 ble.setAdvertisingTimeout(0); // アドバタイジングモードのタイムアウト無効 00144 00145 sd_ble_gap_rssi_start( rxChar.getValueAttribute().getHandle(), 0x00, 0x00 ); 00146 00147 // アドバタイズモード開始 00148 ble.startAdvertising(); 00149 00150 // サービスの追加 00151 ble.addService( txService ); 00152 ble.addService( rxService ); 00153 ble.addService( rssiService ); 00154 00155 // タイマーの設定 00156 Ticker ticker; 00157 ticker.attach( tickerCallback, 0.5 ); 00158 00159 while(1){ 00160 00161 ble.waitForEvent(); 00162 00163 } 00164 }
Generated on Mon Jul 18 2022 14:42:08 by
1.7.2