前提・実現したいこと
Unityの有料アセットの「iBeacon」のサンプルプログラム内で「rssi」と「accuracy」の値をどのように求めているのかを知りたい。そのため、様々な方の知識をお借りしたいと思っております。
一部省略しております。
該当のソースコード
C#
1using UnityEngine; 2using UnityEngine.UI; 3using System; 4using System.Collections; 5using System.Collections.Generic; 6 7internal class Example : MonoBehaviour { 8 [SerializeField] 9 private Text _statusText; 10 11 [SerializeField] 12 private GameObject _statusScreen; 13 14 [SerializeField] 15 private GameObject _menuScreen; 16 17 [SerializeField] 18 private Button _bluetoothButton; 19 20 private Text _bluetoothText; 21 22 /*** Beacon Properties ***/ 23 // Beacontype 24 [SerializeField] //to access in editor, avoiding public varible declaration 25 private Text txt_actualType; 26 // Region 27 [SerializeField] 28 private Text txt_actualRegion; 29 private string s_Region; 30 // UUID, Namespace or Url 31 [SerializeField] 32 private Text txt_actualUUIDChar; 33 [SerializeField] 34 private Text txt_actualUUID; 35 private string s_UUID; 36 // Major/Minor or Instance 37 [SerializeField] 38 private Text txt_actualMajorChar; 39 [SerializeField] 40 private Text txt_actualMajor; 41 private string s_Major; 42 [SerializeField] 43 private Text txt_actualMinorChar; 44 [SerializeField] 45 private Text txt_actualMinor; 46 private string s_Minor; 47 48 /** Input **/ 49 // beacontype 50 [SerializeField] 51 private Dropdown InputDropdown; 52 private BeaconType bt_PendingType; 53 private BeaconType bt_Type; 54 // Region 55 [SerializeField] 56 private InputField input_Region; 57 // UUID, Namespace or Url 58 [SerializeField] 59 private Text txt_inputUUIDChar; 60 [SerializeField] 61 private InputField input_UUID; 62 // Major/Minor or Instance 63 [SerializeField] 64 private Text txt_inputMajorChar; 65 [SerializeField] 66 private InputField input_Major; 67 [SerializeField] 68 private Text txt_inputMinorChar; 69 [SerializeField] 70 private InputField input_Minor; 71 72 // Beacon BroadcastMode (Send, Receive) 73 [SerializeField] 74 private Text txt_BroadcastMode_ButtonText; 75 [SerializeField] 76 private Text txt_BroadcastMode_LabelText; 77 private BroadcastMode bm_Mode; 78 79 // GameObject for found Beacons 80 [SerializeField] 81 private GameObject go_ScrollViewContent; 82 83 // Receive 84 private List<Beacon> mybeacons = new List<Beacon>(); 85 86 private void Start() { 87 setBeaconPropertiesAtStart(); // please keep here! 88 89 _bluetoothButton.onClick.AddListener(delegate() { 90 BluetoothState.EnableBluetooth(); 91 }); 92 93 BluetoothState.BluetoothStateChangedEvent += delegate(BluetoothLowEnergyState state) { 94 switch (state) { 95 case BluetoothLowEnergyState.TURNING_OFF: 96 case BluetoothLowEnergyState.TURNING_ON: 97 break; 98 case BluetoothLowEnergyState.UNKNOWN: 99 case BluetoothLowEnergyState.RESETTING: 100 SwitchToStatus(); 101 _statusText.text = "Checking Device…"; 102 break; 103 case BluetoothLowEnergyState.UNAUTHORIZED: 104 SwitchToStatus(); 105 _statusText.text = "You don't have the permission to use beacons."; 106 break; 107 case BluetoothLowEnergyState.UNSUPPORTED: 108 SwitchToStatus(); 109 _statusText.text = "Your device doesn't support beacons."; 110 break; 111 case BluetoothLowEnergyState.POWERED_OFF: 112 SwitchToMenu(); 113 _bluetoothButton.interactable = true; 114 _bluetoothText.text = "Enable Bluetooth"; 115 break; 116 case BluetoothLowEnergyState.POWERED_ON: 117 SwitchToMenu(); 118 _bluetoothButton.interactable = false; 119 _bluetoothText.text = "Bluetooth already enabled"; 120 break; 121 case BluetoothLowEnergyState.IBEACON_ONLY: 122 SwitchToMenu(); 123 _bluetoothButton.interactable = false; 124 _bluetoothText.text = "iBeacon only"; 125 break; 126 default: 127 SwitchToStatus(); 128 _statusText.text = "Unknown Error"; 129 break; 130 } 131 }; 132 133 } 134 135 // Beacon Properties 136 public void btn_changeUUID() { // onRelease 137 // Beaconregion 138 if (input_Region.text != null && input_Region.text != "") 139 s_Region = input_Region.text; 140 // Beacontype 141 bt_Type = bt_PendingType; 142 // UUID, Namespace, Url 143 if (input_UUID.text != null && input_UUID.text != "") 144 s_UUID = input_UUID.text; 145 // Major or Instance on send 146 if (input_Major .text != null && input_Major.text != "") 147 s_Major = input_Major.text; 148 // Minor on send 149 if (input_Minor .text != null && input_Minor.text != "") 150 s_Minor = input_Minor.text; 151 input_Region.image.color = Color.white; 152 input_Region.text = ""; 153 input_UUID.image.color = Color.white; 154 input_UUID.text = ""; 155 input_Major.image.color = Color.white; 156 input_Major.text = ""; 157 input_Minor.image.color = Color.white; 158 input_Minor.text = ""; 159 InputDropdown.image.color = Color.white; 160 SetBeaconProperties(); 161 SavePlayerPrefs(); 162 } 163 164 // BroadcastMode 165 166 private void SetBroadcastMode() { 167 // setText 168 txt_BroadcastMode_LabelText.text = bm_Mode.ToString(); 169 if (bm_Mode == BroadcastMode.receive) 170 txt_BroadcastMode_ButtonText.text = "Switch to " + BroadcastMode.send.ToString(); 171 else 172 txt_BroadcastMode_ButtonText.text = "Switch to " + BroadcastMode.receive.ToString(); 173 } 174 175 // BroadcastState 176 public void btn_StartStop() { 177 //Debug.Log("Button Start / Stop pressed"); 178 /*** Beacon will start ***/ 179 if (bs_State == BroadcastState.inactive) { 180 // ReceiveMode 181 if (bm_Mode == BroadcastMode.receive) { 182 iBeaconReceiver.BeaconRangeChangedEvent += OnBeaconRangeChanged; 183 // check if all mandatory propertis are filled 184 if (s_Region == null || s_Region == "") { 185 input_Region.image.color= Color.red; 186 return; 187 } 188 if (bt_Type == BeaconType.Any) { 189 iBeaconReceiver.regions = new iBeaconRegion[]{new iBeaconRegion(s_Region, new Beacon())}; 190 } else { 191 if (s_UUID == null || s_UUID == "") { 192 input_UUID.image.color= Color.red; 193 return; 194 } 195 if (bt_Type == BeaconType.iBeacon) { 196 iBeaconReceiver.regions = new iBeaconRegion[]{new iBeaconRegion(s_Region, new Beacon(s_UUID, Convert.ToInt32(s_Major), Convert.ToInt32(s_Minor)))}; 197 } 198 199 } 200 // !!! Bluetooth has to be turned on !!! TODO 201 iBeaconReceiver.Scan(); 202 Debug.Log ("Listening for beacons"); 203 } 204 // SendMode 205 else { 206 // check if all mandatory propertis are filled 207 if (s_Region == null || s_Region == "") { 208 input_Region.image.color= Color.red; 209 return; 210 } 211 else { 212 if (s_UUID == null || s_UUID == "") { 213 input_UUID.image.color= Color.red; 214 return; 215 } 216 else { 217 if (s_Major == null || s_Major == "") { 218 input_Major.image.color= Color.red; 219 return; 220 } 221 else if (bt_Type == BeaconType.iBeacon) { 222 if (s_Minor == null || s_Minor == "") { 223 input_Major.image.color= Color.red; 224 return; 225 } 226 iBeaconServer.region = new iBeaconRegion(s_Region, new Beacon(s_UUID, Convert.ToInt32(s_Major), Convert.ToInt32(s_Minor))); 227 228 } 229 bs_State = BroadcastState.active; 230 img_ButtonBroadcastState.color = Color.red; 231 } else { 232 if (bm_Mode == BroadcastMode.receive) {// Stop for receive 233 iBeaconReceiver.Stop(); 234 iBeaconReceiver.BeaconRangeChangedEvent -= OnBeaconRangeChanged; 235 removeFoundBeacons(); 236 } else { // Stop for send 237 iBeaconServer.StopTransmit(); 238 } 239 bs_State = BroadcastState.inactive; 240 img_ButtonBroadcastState.color = Color.green; 241 } 242 SetBroadcastState(); 243 SavePlayerPrefs(); 244 } 245 246 private void OnBeaconRangeChanged(Beacon[] beacons) { // 247 foreach (Beacon b in beacons) { 248 var index = mybeacons.IndexOf(b); 249 if (index == -1) { 250 mybeacons.Add(b); 251 } else { 252 mybeacons[index] = b; 253 } 254 } 255 for (int i = mybeacons.Count - 1; i >= 0; --i) { 256 if (mybeacons[i].lastSeen.AddSeconds(10) < DateTime.Now) { 257 mybeacons.RemoveAt(i); 258 } 259 } 260 DisplayOnBeaconFound(); 261 } 262 263 private void DisplayOnBeaconFound() { 264 removeFoundBeacons(); 265 RectTransform rt_Content = (RectTransform)go_ScrollViewContent.transform; 266 foreach (Beacon b in mybeacons) { 267 // create clone of foundBeacons 268 go_FoundBeaconClone = Instantiate(go_FoundBeacon); 269 // make it child of the ScrollView 270 go_FoundBeaconClone.transform.SetParent(go_ScrollViewContent.transform); 271 // get resolution based scalefactor 272 float f_scaler = ((RectTransform)go_FoundBeaconClone.transform).localScale.y; 273 Vector2 v2_scale = new Vector2(1,1); 274 // reset scalefactor 275 go_FoundBeaconClone.transform.localScale = v2_scale; 276 // get anchorposition 277 Vector3 pos = go_ScrollViewContent.transform.position; 278 // positioning 279 pos.y -= f_ScrollViewContentRectHeight/f_scaler * i_BeaconCounter; 280 go_FoundBeaconClone.transform.position = pos; 281 i_BeaconCounter++; 282 // resize scrollviewcontent 283 rt_Content.sizeDelta = new Vector2(f_ScrollViewContentRectWidth,f_ScrollViewContentRectHeight*i_BeaconCounter); 284 go_FoundBeaconClone.SetActive(true); 285 // adding reference to instance 286 go_FoundBeaconCloneList.Add(go_FoundBeaconClone); 287 // get textcomponents 288 Text[]Texts = go_FoundBeaconClone.GetComponentsInChildren<Text>(); 289 // deleting placeholder 290 foreach (Text t in Texts) 291 t.text = ""; 292 Debug.Log ("fond Beacon: " + b.ToString()); 293 switch (b.type) { 294 case BeaconType.iBeacon: 295 Texts[0].text = "UUID:"; 296 Texts[1].text = b.UUID.ToString(); 297 Texts[2].text = "Major:"; 298 Texts[3].text = b.major.ToString(); 299 Texts[4].text = "Minor:"; 300 Texts[5].text = b.minor.ToString(); 301 Texts[6].text = "Range:"; 302 Texts[7].text = b.range.ToString(); 303 Texts[8].text = "Strength:"; 304 Texts[9].text = b.strength.ToString() + " db"; 305 Texts[10].text = "Accuracy:"; 306 Texts[11].text = b.accuracy.ToString().Substring(0,10) + " m"; 307 Texts[12].text = "Rssi:"; 308 Texts[13].text = b.rssi.ToString() + " db"; 309 break; 310 311 //省略 312 default: 313 break; 314 } 315 } 316 } 317}
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。