delphiXE开发蓝牙BLE4.0程序时遇到的notification问题

IDE环境delphiXE8

蓝牙硬件ST17H26

service:0xfee7

chareter:0xfec9

 

const u16 my_OEMServiceUUID=0xfee7;
const u16 my_OEMCharaterUUID=0xfec9;

在my_Attributes[]中添加

{4,2,2,(u8*)(&my_primaryServiceUUID), (u8*)(&my_OEMServiceUUID)},
{0,2,1,(u8*)(&my_characterUUID), (u8*)(&PROP_READ_WRITE_NORSP_NOTIFY)},
{0,2,1, (u8*)(&my_OEMCharaterUUID), (u8*)(&my_page)},

 

手机调试软件BLEReader可以正常看到service和charater,点击"开始通知",蓝牙端发送可以正常接收。

delphixe内容:

procedure TForm1.Timer1Timer(Sender: TObject);
begin
case Step of
  1:
    try
    FBluetoothManagerLE := TBluetoothLEManager.Current;
    FAdapter := FBluetoothManagerLE.CurrentAdapter;
    if ManagerConnected then
    begin
      FBluetoothManagerLE.OnDiscoveryEnd := BluetoothLE1EndDiscoverDevices;
      FBluetoothManagerLE.StartDiscovery(1000, ADeviceUUIDList);
    end;
   except
    on E: Exception do  ShowMessage('step1'+E.Message);
  end;
  2:
  try
         CurrDevice.OnServicesDiscovered := ServicesDiscovered;

        if (FServiceDiscovered = false) then  CurrDevice.DiscoverServices;
  except
    on E: Exception do  ShowMessage('step2'+E.Message);
  end;
  3:
  try
    FChar1 := FService.GetCharacteristic(StringToGUID(UUID_CHAR1));
    if FChar1<>nil then
    begin
    CurrDevice.OnCharacteristicRead := OnCharRead;
    CurrDevice.SetCharacteristicNotification(FChar1,true);
    CurrDevice.ReadCharacteristic(FChar1);
    FChar1.GetDescriptor()
    Memo1.Lines.Add('找到键值');
    Step:=4;
    end else Memo1.Lines.Add('aaa');
   except
    on E: Exception do  ShowMessage('step3'+E.Message);
  end;
  4:
  ;
end;
end;
在SetCharacteristicNotification后,无法从server端获得notify,跟踪到源文件中:
function TBluetoothLEDevice.SetCharacteristicNotification(const ACharacteristic: TBluetoothGattCharacteristic;
  Enable: Boolean): Boolean;
const
  Desc_Configuration: TBluetoothUUID = '{00002902-0000-1000-8000-00805F9B34FB}';
var
  LDesc: TBluetoothGattDescriptor;
begin
  Result := False;
  if [TBluetoothProperty.Notify, TBluetoothProperty.Indicate] * ACharacteristic.GetProperties <> [] then
  begin
    // This is to ensure that we have read the descriptors before querying.
    ACharacteristic.Descriptors;
    // We check that we have the Configurarion descriptor, and then we set the notification accordingly.
    LDesc := ACharacteristic.GetDescriptor(Desc_Configuration);
    if LDesc <> nil then
      Result := DoSetCharacteristicNotification(ACharacteristic, Enable);
  end;
end;
可以发现,在真正设置
DoSetCharacteristicNotification(ACharacteristic, Enable);前进行了一些列的判断,注意是要求该charater下要有个
Desc_Configuration的UUID,如果没有就不会实质上地设置notify。于是回过头来修改蓝牙代码:
增加了:

{0,2,2,(u8*)(&clientCharacterCfgUUID), (u8*)(serviceChangeCCC)},

修改mac(不知道为什么,不修改反映不出变化)

重新烧录后,0xfec9下面多了一个0x2902的describe。再次运行delphi端程序,可以得到notify,问题解决。

posted @ 2017-03-20 19:03  csaaa  阅读(1799)  评论(0编辑  收藏  举报