#!/bin/bash
if [ `lsusb | grep "7392:a611" | wc -l` != 0 ];
then
   MODE=$1
   NAME=$2
   LEN=`echo ${#NAME}`
   LEN=$((LEN+1))
   LEN=`printf "%02x\n" $LEN`
   NAME=`echo -n $NAME | xxd -p`
   MSG="020A08"$LEN"09"$NAME" 1"
   BLE_GATT_SERVER="/usr/bin/gatt_server"
   btmgmt -i hci0 power off
   echo 3200 > /sys/kernel/debug/bluetooth/hci0/supervision_timeout
   btmgmt -i hci0 le on
   btmgmt -i hci0 connectable on
   btmgmt add-adv -d $MSG
   if [ "$MODE" == "bleonly" ]
   then
      btmgmt -i hci0 bredr off
      btmgmt -i hci0 bondable off
   else 
      btmgmt -i hci0 bredr on	   
      btmgmt -i hci0 bondable on
   fi
   btmgmt -i hci0 power on
   if [ `ps ax | grep "$BLE_GATT_SERVER" | grep -v grep | wc -l` == 0 ];
   then
      echo "Start BLE GATT server"
      python3 $BLE_GATT_SERVER &
   else
      echo "BLE GAT server already running"
   fi
fi

