Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Assets/CoreBluetooth/Samples/12_Debug/SampleDebug_Central.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,16 @@ public void DidUpdateNotificationStateForCharacteristic(CBPeripheral peripheral,
}
}

public void DidReadRSSI(CBPeripheral peripheral, int rssi, CBError error)
{
Debug.Log($"[DidReadRSSI] rssi: {rssi}");
if (error != null)
{
Debug.LogError($"[DidReadRSSI] error: {error}");
return;
}
}

public void OnClickWrite()
{
if (_peripheral == null)
Expand Down Expand Up @@ -181,6 +191,17 @@ public void OnClickRead()
_peripheral.ReadValue(_remoteCharacteristic);
}

public void OnClickReadRSSI()
{
if (_peripheral == null)
{
Debug.Log("peripheral is null.");
return;
}

_peripheral.ReadRSSI();
}

void OnDestroy()
{
if (_centralManager != null)
Expand Down
213 changes: 213 additions & 0 deletions Assets/CoreBluetooth/Samples/12_Debug/SampleDebug_CentralScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,139 @@ CanvasRenderer:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 167323285}
m_CullTransparentMesh: 1
--- !u!1 &185931679
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 185931680}
- component: {fileID: 185931683}
- component: {fileID: 185931682}
- component: {fileID: 185931681}
m_Layer: 5
m_Name: ReadRSSIButton
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &185931680
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 185931679}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children:
- {fileID: 1469846735}
m_Father: {fileID: 994619686}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: 363, y: -99.8}
m_SizeDelta: {x: 320, y: 60}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &185931681
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 185931679}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 4e29b1a8efbd4b44bb3f3716e73f07ff, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Navigation:
m_Mode: 3
m_WrapAround: 0
m_SelectOnUp: {fileID: 0}
m_SelectOnDown: {fileID: 0}
m_SelectOnLeft: {fileID: 0}
m_SelectOnRight: {fileID: 0}
m_Transition: 1
m_Colors:
m_NormalColor: {r: 1, g: 1, b: 1, a: 1}
m_HighlightedColor: {r: 0.9607843, g: 0.9607843, b: 0.9607843, a: 1}
m_PressedColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 1}
m_SelectedColor: {r: 0.9607843, g: 0.9607843, b: 0.9607843, a: 1}
m_DisabledColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 0.5019608}
m_ColorMultiplier: 1
m_FadeDuration: 0.1
m_SpriteState:
m_HighlightedSprite: {fileID: 0}
m_PressedSprite: {fileID: 0}
m_SelectedSprite: {fileID: 0}
m_DisabledSprite: {fileID: 0}
m_AnimationTriggers:
m_NormalTrigger: Normal
m_HighlightedTrigger: Highlighted
m_PressedTrigger: Pressed
m_SelectedTrigger: Selected
m_DisabledTrigger: Disabled
m_Interactable: 1
m_TargetGraphic: {fileID: 185931682}
m_OnClick:
m_PersistentCalls:
m_Calls:
- m_Target: {fileID: 1378470629}
m_TargetAssemblyTypeName: CoreBluetoothSample.SampleDebug_Central, Assembly-CSharp
m_MethodName: OnClickReadRSSI
m_Mode: 1
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
--- !u!114 &185931682
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 185931679}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 1
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_Sprite: {fileID: 10905, guid: 0000000000000000f000000000000000, type: 0}
m_Type: 1
m_PreserveAspect: 0
m_FillCenter: 1
m_FillMethod: 4
m_FillAmount: 1
m_FillClockwise: 1
m_FillOrigin: 0
m_UseSpriteMesh: 0
m_PixelsPerUnitMultiplier: 1
--- !u!222 &185931683
CanvasRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 185931679}
m_CullTransparentMesh: 1
--- !u!1 &268526162
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -843,6 +976,7 @@ RectTransform:
m_Children:
- {fileID: 1464279507}
- {fileID: 2085064255}
- {fileID: 185931680}
- {fileID: 1878487716}
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
Expand Down Expand Up @@ -1028,6 +1162,85 @@ CanvasRenderer:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1464279506}
m_CullTransparentMesh: 1
--- !u!1 &1469846734
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1469846735}
- component: {fileID: 1469846737}
- component: {fileID: 1469846736}
m_Layer: 5
m_Name: Text (Legacy)
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &1469846735
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1469846734}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 185931680}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 1, y: 1}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 0, y: 0}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &1469846736
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1469846734}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 5f7201a12d95ffc409449d95f23cf332, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 0.19607843, g: 0.19607843, b: 0.19607843, a: 1}
m_RaycastTarget: 1
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_FontData:
m_Font: {fileID: 10102, guid: 0000000000000000e000000000000000, type: 0}
m_FontSize: 31
m_FontStyle: 0
m_BestFit: 0
m_MinSize: 10
m_MaxSize: 40
m_Alignment: 4
m_AlignByGeometry: 0
m_RichText: 1
m_HorizontalOverflow: 0
m_VerticalOverflow: 0
m_LineSpacing: 1
m_Text: Read RSSI
--- !u!222 &1469846737
CanvasRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1469846734}
m_CullTransparentMesh: 1
--- !u!1 &1878487715
GameObject:
m_ObjectHideFlags: 0
Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ void DidUpdateValueForCharacteristic(CBPeripheral peripheral, CBCharacteristic c
void DidWriteValueForCharacteristic(CBPeripheral peripheral, CBCharacteristic characteristic, CBError error) { }
void IsReadyToSendWriteWithoutResponse(CBPeripheral peripheral) { }
void DidUpdateNotificationStateForCharacteristic(CBPeripheral peripheral, CBCharacteristic characteristic, CBError error) { }
void DidReadRSSI(CBPeripheral peripheral, int rssi, CBError error) { }
}

/// <summary>
Expand Down Expand Up @@ -152,6 +153,15 @@ public bool CanSendWriteWithoutResponse
}
}

/// <summary>
/// Retrieves the current RSSI value for the peripheral while connected to the central manager.
/// </summary>
public void ReadRSSI()
{
ExceptionUtils.ThrowObjectDisposedExceptionIf(_disposed, this);
_nativePeripheral.ReadRSSI();
}

internal CBCharacteristic FindCharacteristic(string serviceUUID, string characteristicUUID)
{
if (string.IsNullOrEmpty(serviceUUID))
Expand Down Expand Up @@ -231,6 +241,12 @@ void INativePeripheralDelegate.DidUpdateNotificationStateForCharacteristic(strin
Delegate?.DidUpdateNotificationStateForCharacteristic(this, characteristic, error);
}

void INativePeripheralDelegate.DidReadRSSI(int rssi, CBError error)
{
if (_disposed) return;
Delegate?.DidReadRSSI(this, rssi, error);
}

public override string ToString()
{
return $"CBPeripheral: identifier = {Identifier}, name = {Name}, state = {State}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ int serviceUUIDsCount
internal delegate void CB4UPeripheralDidWriteValueForCharacteristicHandler(IntPtr peripheralPtr, IntPtr serviceUUIDPtr, IntPtr characteristicUUIDPtr, int errorCode);
internal delegate void CB4UPeripheralIsReadyToSendWriteWithoutResponseHandler(IntPtr peripheralPtr);
internal delegate void CB4UPeripheralDidUpdateNotificationStateForCharacteristicHandler(IntPtr peripheralPtr, IntPtr serviceUUIDPtr, IntPtr characteristicUUIDPtr, int notificationState, int errorCode);
internal delegate void CB4UPeripheralDidReadRSSIHandler(IntPtr peripheralPtr, int rssi, int errorCode);

[DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
internal static extern void cb4u_peripheral_register_handlers(
Expand All @@ -85,7 +86,8 @@ internal static extern void cb4u_peripheral_register_handlers(
CB4UPeripheralDidUpdateValueForCharacteristicHandler didUpdateValueForCharacteristicHandler,
CB4UPeripheralDidWriteValueForCharacteristicHandler didWriteValueForCharacteristicHandler,
CB4UPeripheralIsReadyToSendWriteWithoutResponseHandler isReadyToSendWriteWithoutResponseHandler,
CB4UPeripheralDidUpdateNotificationStateForCharacteristicHandler didUpdateNotificationStateForCharacteristicHandler
CB4UPeripheralDidUpdateNotificationStateForCharacteristicHandler didUpdateNotificationStateForCharacteristicHandler,
CB4UPeripheralDidReadRSSIHandler didReadRSSIHandler
);

[DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
Expand Down Expand Up @@ -152,6 +154,9 @@ internal static extern int cb4u_peripheral_set_notify_value(
[return: MarshalAs(UnmanagedType.I1)]
internal static extern bool cb4u_peripheral_can_send_write_without_response(SafeNativePeripheralHandle handle);

[DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
internal static extern void cb4u_peripheral_read_rssi(SafeNativePeripheralHandle handle);

[DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
internal static extern int cb4u_peripheral_characteristic_properties(
SafeNativePeripheralHandle handle,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,5 +140,10 @@ internal bool CanSendWriteWithoutResponse
return NativeMethods.cb4u_peripheral_can_send_write_without_response(_handle);
}
}

internal void ReadRSSI()
{
NativeMethods.cb4u_peripheral_read_rssi(_handle);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ void DidUpdateValueForCharacteristic(string serviceUUID, string characteristicUU
void DidWriteValueForCharacteristic(string serviceUUID, string characteristicUUID, CBError error) { }
void IsReadyToSendWriteWithoutResponse() { }
void DidUpdateNotificationStateForCharacteristic(string serviceUUID, string characteristicUUID, bool enabled, CBError error) { }
void DidReadRSSI(int rssi, CBError error) { }
}

internal class SafeNativePeripheralHandle : SafeHandle
Expand All @@ -34,7 +35,8 @@ void RegisterHandlers()
DidUpdateValueForCharacteristic,
DidWriteValueForCharacteristic,
IsReadyToSendWriteWithoutResponse,
DidUpdateNotificationStateForCharacteristic
DidUpdateNotificationStateForCharacteristic,
DidReadRSSI
);
}

Expand Down Expand Up @@ -130,5 +132,14 @@ internal static void DidUpdateNotificationStateForCharacteristic(IntPtr peripher
CBError.CreateOrNullFromCode(errorCode)
);
}

[AOT.MonoPInvokeCallback(typeof(NativeMethods.CB4UPeripheralDidReadRSSIHandler))]
internal static void DidReadRSSI(IntPtr peripheralPtr, int rssi, int errorCode)
{
GetDelegate(peripheralPtr)?.DidReadRSSI(
rssi,
CBError.CreateOrNullFromCode(errorCode)
);
}
}
}
Loading