コード例 #1
0
    def send_packets_to_android(self, ibus_packets):
        """
        Sends data via Bluetooth socket connection
        """
        if self.conn_established:
            try:
                print("Sending encapsulated IBUSPacket(s)...")
                packets = []
                for ibus_packet in ibus_packets:
                    packets.append(ibus_packet.as_dict())

                # encapsulate IBUS packet in BlueBUSPacket
                packet = BlueBUSPacket(packet_type=BlueBUSPacket.TYPE_PACKET,
                                       data=json.dumps(packets))

                # serialize BlueBusPacket and send
                json_data = json.dumps(packet.as_dict())
                if globals.debug:
                    print json_data
                self.client_sock.send(json_data)

                # packet sent successfully
                #print packet
                return True
            except Exception as e:
                # socket was closed, graceful restart
                print "Error: " + e.message
                globals.restart_bluetooth()
                return False
コード例 #2
0
    def process_data_from_android(data):
        """
        Processes received data from Bluetooth socket
        """
        try:
            # inflate JSON data to BlueBUSPacket
            parsed_json = json.loads(data)
            packet = BlueBUSPacket(packet_type=parsed_json['type'],
                                   data=parsed_json['data'])
            print("Received BlueBusPacket from Android [" +
                  str(len(packet.data)) + "]")

            # check if command to perform (i.e. write to IBUS)
            globals.ibus_service.write_to_ibus(packet.data.decode('hex'))
            return
        except Exception as e:
            print "Error: " + e.message
            return
コード例 #3
0
    def send_command_to_android(self, command):
        """
        Sends data via Bluetooth socket connection
        """
        try:
            print("Sending message...")
            # encapsulate command in BlueBUSPacket
            packet = BlueBUSPacket(packet_type=BlueBUSPacket.TYPE_COMMAND,
                                   data=command)

            # serialize BlueBusPacket and send
            json_data = json.dumps(packet.__dict__())
            self.client_sock.send(json_data)
            #print packet
            return True
        except Exception as e:
            print "Error: " + e.message
            globals.restart_bluetooth()
            return False