我有2个双h桥接控制器(用于为风扇供电)通过GPIO连接到我的覆盆子pi 3 b和电池供电我在python 3中创建了一个脚本,使用蓝牙经典来询问连接的手机一个终端风扇需要多长时间以及它们应该在一个循环中关闭多长时间我有这个工作很棒,但这只适用于Android,因为它是蓝牙经典我现在想让这个工作为我的iPhone我发现我需要使用BLE所以我的问题是如何将我的原始脚本转换为蓝牙经典(使用RF COMM套接字)以使用蓝牙低功耗 .

这是我的原始剧本

# Importing the Bluetooth Socket library
import bluetooth
# Importing the GPIO library to use the GPIO pins of Raspberry pi
import RPi.GPIO as GPIO
import time

fan_pin = 16# Initializing pin 16 for fan
fan2_pin = 18
GPIO.setmode(GPIO.BOARD)    # Using BCM numbering
GPIO.setup(fan_pin, GPIO.OUT)   # Declaring the pin 16 as output pin
GPIO.setup(fan2_pin, GPIO.OUT)  # Declaring the pin 16 as output pin

host = ""
port = 1    # Raspberry Pi uses port 1 for Bluetooth Communication

# Creaitng Socket Bluetooth RFCOMM communication
server = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
print('Bluetooth Socket Created')

try:
    server.bind((host, port))
    print("Bluetooth Binding Completed")
except:
    print("Bluetooth Binding Failed")

server.listen(1) # One connection at a time
# Server accepts the clients request and assigns a mac address. 
client, address = server.accept()
print("Connected To", address)
print("Client:", client)
while 1:


    # Receivng the data. 
    client.send("How long do you want the fans to be on for?/n")
    On = int(client.recv(1024)) # 1024 is the buffer size.
    time.sleep(5)
    client.send("Ok, Now how long do you want the fans to be off for?/n")
    Off = int(client.recv(1024))
    print(On)
    print(Off)
    while True:
        GPIO.output(fan_pin, GPIO.HIGH)
        GPIO.output(fan2_pin, GPIO.HIGH)
        time.sleep(On)
        GPIO.output(fan_pin, GPIO.LOW)
        GPIO.output(fan2_pin, GPIO.LOW)
        time.sleep(Off)



# Making all the output pins LOW
GPIO.cleanup()
# Closing the client and server connection
client.close()
server.close()

我认为这就像将蓝牙经典代码更改为BLE代码一样简单 . 最大20行改变?